🔄 Quick Recap
So far, we’ve learned:
-
The CPU is lightning fast but needs helpers (RAM and cache) to keep up.
-
Cache is the fastest, smallest memory inside the CPU.
-
RAM is the CPU’s working desk — fast, but temporary.
-
The CPU uses buses (address, data, control) to read and write information from RAM.
But what if… RAM gets full? 🤔
That’s what this lesson is about.
🧩 The Problem: RAM Isn’t Infinite
RAM is big, but not endless.
-
A computer might have 8 GB of RAM.
-
A big game or video editor might need 10 GB of memory.
So where does the extra 2 GB go?
Does the computer just crash?
Not exactly. Computers are clever — they use a trick called virtual memory.
🧠 Virtual Memory — Borrowing Space
When RAM runs out, the CPU asks the operating system (Windows, macOS, Linux, etc.) for help.
The operating system says:
“Don’t worry, I’ll borrow some space from the storage drive (hard drive or SSD) and pretend it’s more RAM.”
This borrowed space is called a swap file or page file.
So now, the computer acts like it has more RAM than it really does.
That’s why it’s called virtual memory — it’s not real RAM, but it works like it.
⚡ Why Virtual Memory Is Slower
Remember: storage is much slower than RAM.
-
RAM: nanoseconds (billionths of a second).
-
SSD: microseconds (millionths of a second).
-
Hard drive: milliseconds (thousandths of a second).
So when the CPU has to use virtual memory, it slows down.
That’s why your computer feels laggy when too many apps are open.
🔄 Paging — Moving Chunks In and Out
The computer doesn’t move data one bit at a time. That would be too slow.
Instead, it moves data in chunks called pages (usually 4 KB each).
-
If RAM is full, the operating system takes a page of data the CPU doesn’t need right now, and writes it to storage.
-
Later, if the CPU needs it again, the operating system swaps it back into RAM.
This back-and-forth is called paging or swapping.
🚦 Page Faults — When the CPU Has to Wait
What if the CPU asks for something that’s no longer in RAM because it was swapped out?
That’s called a page fault.
It means:
-
The CPU has to pause.
-
The operating system fetches the page from storage.
-
Then puts it back into RAM so the CPU can continue.
This is why computers sometimes “freeze” for a moment when memory is low.
📍 Real-Life Analogy
Imagine your desk (RAM) is full of books.
-
If you want to read a new book, but the desk is full, you put one of the old books back in the backpack (storage).
-
When you need that book again, you stop, pull it from your backpack, and put it back on your desk.
That stop-and-swap process is just like paging.
The more you swap books, the slower your homework goes.
🧠 What If RAM Is Really Too Small?
If your RAM is much smaller than the programs you’re running, your computer will:
-
Swap pages constantly.
-
Get very slow (this is called thrashing).
-
Sometimes crash if it can’t keep up.
That’s why more RAM usually makes computers run smoother.
📚 Recap
-
RAM isn’t infinite — when it’s full, the computer uses virtual memory on storage.
-
Data is swapped in chunks called pages.
-
If the CPU needs a page that’s not in RAM, it causes a page fault, and the operating system brings it back.
-
Using storage as RAM is much slower, but it keeps the computer running instead of crashing.
-
More RAM = fewer swaps = faster computer.
Isn’t this exciting? Let’s Go Even More Deep Now!
Sometimes your computer wants to remember more than the RAM can hold. (RAM = the fast “work table.”) When that happens, the computer does not give up. It uses smart tricks so work can continue. Today we learn those tricks in detail.
We will meet these helpers:
-
Virtual memory
-
Pages and frames
-
Page tables
-
MMU (Memory Management Unit)
-
TLB (Translation Lookaside Buffer) – a tiny, fast page-map cache
-
Page faults
-
Dirty and clean pages
-
Copy-on-write
-
Read-ahead and write-back
-
Thrashing and working set
-
Huge pages (big pages for speed)
Don’t worry. Each one will be explained simply and exactly.
1) The big idea: Virtual memory
Virtual memory means: each program pretends it has a big, neat memory all for itself.
-
“Virtual” = pretend space your program sees.
-
“Physical” = the real RAM chips on the board.
Why pretend?
-
It keeps programs safe from each other.
-
It lets the computer borrow space from the fast disk (SSD) when RAM is full.
-
It keeps memory tidy for each program.
2) Pages and frames (the unit of travel)
Memory moves in blocks.
-
A page is a block in virtual memory.
-
A frame is a block in physical RAM.
-
A common size is 4 KB. (There are also huge pages like 2 MB or 1 GB to reduce overhead.)
So the job is: map a virtual page → to a physical frame.
3) Page tables: the map book
A page table is a big table (a list) that says:
-
“Virtual page number 123 → physical frame number 789”
-
Plus tiny notes (called bits) about each page:
-
Present/Valid: is it in RAM right now?
-
Read/Write/Execute: what are we allowed to do with it?
-
Dirty: did we change it?
-
Accessed/Referenced: did we use it recently?
-
User/Supervisor: can a normal app touch it, or only the system?
-
Multi-level page tables: Instead of one huge map, the map is split into smaller books (levels) to save space. The computer walks these levels to find the final answer.
4) Memory Management Unit (MMU): the translator chip
The MMU (Memory Management Unit) is a tiny helper next to the CPU.
Its job: translate virtual addresses (pretend) to physical addresses (real).
Every time the CPU asks, “Give me what lives at virtual page X,” the MMU looks it up using the page tables.
5) Translation Lookaside Buffer (TLB): a tiny, extra-fast map cache
Looking in page tables every time would be slow.
So the Memory Management Unit (MMU( keeps a tiny, very fast list called the TLB (Translation Lookaside Buffer).
-
Translation Lookaside Buffer (TLB) hit: the mapping is found here → very fast.
-
Translation Lookaside Buffer (TLB) miss: not found → must walk the page tables → slower (but still fine).
Operating systems try to keep the Translation Lookaside Buffer (TLB) happy by keeping common pages around.
6) When RAM is full: the swap file (page file) 🧩
If RAM has no free frames left, the system uses virtual memory with a swap file (also called page file) on the SSD/HDD.
-
Pages that are not needed right now can be written to the swap file.
-
Later, if needed again, the page is read back into RAM.
This keeps work going, but slower, because SSD/HDD is slower than RAM.
7) What is a page fault? 🚦
A page fault happens when the CPU asks for a virtual page that is not in RAM right now.
Step-by-step:
-
CPU asks Memory Management Unit (MMU): “Where is virtual page V?”
-
MMU/TLB cannot find it in RAM → page fault.
-
The operating system pauses the program briefly.
-
It chooses a frame in RAM to free. (If that frame’s page is dirty—changed—save it to disk first. If clean—unchanged—just reuse the frame.)
-
It reads the needed page from disk into that frame.
-
It updates the page table entry.
-
It tells the CPU to try again → now it will work.
Dirty vs clean:
-
Dirty page = changed since loaded. Must be written back to disk before reuse.
-
Clean page = not changed. Can be dropped and refetched later if needed.
8) How does the Operating System choose which page to move out?
It tries to pick a page you won’t need soon. This is hard!
Common idea: “Least Recently Used (LRU)” — throw out what hasn’t been used lately.
Real systems use approximations of LRU (like Clock algorithm) using the Accessed/Referenced bit.
Working set: the set of pages a program is touching right now. If your RAM can fit your working set, your program feels fast. If not, the system swaps too much.
Thrashing: when the system spends most of its time swapping pages in and out instead of doing real work. Everything feels very slow.
9) Read-ahead and write-back (go faster tricks)
Read-ahead (prefetch):
If you read page 20, the operating system may also load page 21 because you might need it next (like turning to the next page in a book). This reduces future page faults.
Write-back:
Instead of writing every tiny change to disk right away, the operating system keeps the page in RAM marked dirty and writes it later (in batches). This is faster. (If power dies unexpectedly, that’s why we use safe shutdowns and journaling.)
Write-through:
Always write changes to disk immediately. Safer but slower. Most systems favor write-back plus safety tools.
10) Copy-on-write (save memory smartly)
Sometimes two programs (or a program and its child) share the same page on purpose.
-
If neither changes it, they keep sharing.
-
If one writes to the page, then the Operating System makes a new private copy for the writer.
This is called copy-on-write (COW). It saves RAM when many programs use the same data or code.
11) Memory-mapped files (fast file access)
A memory-mapped file lets a file on disk appear as pages in a program’s virtual memory.
-
Read the file? It’s just like reading memory.
-
Need new parts? The Operating System brings the needed pages in.
This avoids extra copying and can be very fast.
12. 🧺 Stack, Heap, and Fragmentation
Your program’s memory is like a playground with two special zones:
-
The Stack (quick and tidy)
-
The stack is like a pile of trays in a cafeteria.
-
When you call a function (ask the computer to do a job), it puts a new tray on top with the info it needs (like numbers, steps, or places to return).
-
When the function ends, that tray is taken off.
-
This is fast because you only add/remove from the top of the pile.
-
But stacks are not good for long-term storage — just quick tasks.
-
-
The Heap (messy but flexible)
-
The heap is like a toy box where you can throw in toys anywhere.
-
You can ask: “Please give me a box big enough to hold 20 Lego pieces.”
-
Later you can also ask: “Now I need a smaller box for 3 pieces.”
-
The heap gives you boxes of the right size, wherever it finds space.
-
Over time, if you keep taking boxes in and out, little holes appear — that’s called fragmentation.
-
Fragmentation:
-
Imagine your toy box has holes of empty space that are too small for your new toy.
-
The total space might be enough, but because it’s broken into pieces, you can’t use it well.
-
The operating system and runtime try to shuffle or manage things so space doesn’t get wasted.
📏 Huge Pages (Why Bigger Can Be Faster)
Normally, memory is divided into pages of about 4 KB each.
-
Every page has to be tracked in the page tables (the big map of memory).
-
If you’re using gigabytes of memory, that’s millions of pages to track!
-
Looking them up takes time, and the TLB (tiny memory map cache) can’t hold them all.
Huge Pages solve this:
-
Instead of 4 KB, a page might be 2 MB or even 1 GB.
-
That means fewer pages to keep track of.
-
Fewer pages = fewer Translation Lookaside Buffer (TLB) misses = faster access.
When are huge pages useful?
-
For programs that use giant chunks of memory without stopping — like databases, scientific simulations, or AI training.
-
Not always needed for small apps, because big pages can waste memory if you don’t fill them up.
🌟 Recap of Both
-
Stack = pile of trays, fast, grows/shrinks with function calls.
-
Heap = toy box, flexible, but can get messy (fragmentation).
-
Fragmentation = when little gaps waste space in the heap.
-
Normal pages = small (4 KB), easy to manage, but too many can slow things down.
-
Huge pages = big (2 MB or 1 GB), fewer to manage, faster for big jobs.