https://www.youtube.com/watch?v=XZFdkQ_y0-M
Your program thinks it owns all the memory on your computer. It doesn't. Not even close.
When your code allocates an array, it gets an address like 0x7FFF0000. It reads from it, writes to it, treats it like physical RAM. But that address is a lie. It doesn't point to a real location in hardware. It points to a virtual address — a fake address your operating system invented.
Every program on your machine lives in its own virtual address space. Each one believes it has 256 terabytes of memory all to itself. On a machine with 16 GB of actual RAM.
This is virtual memory. And without it, modern computing wouldn't exist.
Pages: The Unit of Illusion
The OS doesn't map individual bytes — that would require a translation table bigger than memory itself. Instead, it splits memory into fixed-size chunks called pages, usually 4 KB each.
Your program's virtual space is divided into virtual pages. Physical RAM is divided into physical frames. The OS maintains a mapping between them:
→ Virtual page 12 maps to physical frame 47
→ Virtual page 13 maps to physical frame 3
→ Virtual page 0 maps to physical frame 900
The pages don't have to be in order. Your program sees a clean, contiguous address space. Underneath, its data is scattered across RAM like puzzle pieces. Programs don't need contiguous physical memory — they just need the illusion of it.
The Page Table
The mapping lives in a data structure called the page table. Every process has its own.
On every memory access, the CPU splits the address into two parts: the upper bits identify the virtual page number, the lower bits identify the offset within that page. The CPU looks up the page number in the page table, finds the corresponding physical frame, combines it with the offset — and that's the real physical address.
This happens billions of times per second. Every instruction fetch. Every variable read. Every pointer dereference.
But there's a problem. If the page table lives in memory, then every memory access requires two memory accesses — one to read the table, one to read the data. That doubles your latency.
The TLB: Making It Fast
The Translation Lookaside Buffer is a tiny cache built directly into the CPU. It stores recent page table entries — typically just 64 to 1,500 of them. Tiny compared to the full page table. But it has a hit rate above 99%.
→ TLB hit: translation in a single clock cycle, no memory access needed
→ TLB miss: walk the page table in memory, 10-100 clock cycles
Why does such a small cache work so well? Locality. Programs access the same pages over and over. A loop iterating over an array hits the same few pages thousands of times. The TLB remembers them, and translation becomes invisible.
What Happens When RAM Runs Out
Your laptop has 16 GB. But Chrome with 40 tabs, Slack, Spotify, VS Code, and a Docker container want way more than that combined.
Virtual memory handles this too. The OS picks a page that hasn't been used recently, writes it to disk (swap space), and frees the physical frame for whoever needs it. If the original program tries to access that evicted page, the CPU triggers a page fault. The OS reads it back from disk, updates the page table, and the program continues — never knowing anything happened.
But disk is about 1,000x slower than RAM. If too many pages are being swapped, your system starts thrashing. Everything grinds to a halt. That's why your computer slows to a crawl when you open too many programs.
Why It's the Foundation of Everything
Virtual memory isn't just a convenience. It enables:
→ Process isolation — a bug in Chrome can't corrupt Spotify's memory. Page tables are separate, so programs can't even see each other.
→ Shared libraries — 100 programs sharing one copy of libc instead of separate copies. Saves gigabytes.
→ Memory-mapped files — map a file directly into your address space. Reads become pointer dereferences. Databases love this.
→ Lazy allocation — allocate a gigabyte, but only get physical frames when you actually touch each page. Programs routinely allocate way more than they use, and that's fine.
Virtual memory is a lie your CPU tells every program. And it makes everything possible.
Watch the full animated breakdown: Virtual Memory: Your Program Lives in a Fantasy World
Neural Download — visual mental models for computer science and machine learning.
Top comments (0)