Virtual Memory
Memory Fundamentals · Memory & Systems track
6 min read
In this module
The Illusion of Unlimited Memory
A running programme acts like it owns an entire memory space, starting at address 0 and stretching up to billions. The whole thing is theatre. The programme is using virtual addresses, and the real, physical memory is being shared between every process on the machine. The mechanism keeping that illusion alive is virtual memory.
Physical vs. Virtual Addresses
When the CPU runs your code it generates virtual addresses. A hardware unit called the Memory Management Unit (MMU) translates each virtual address to a physical address in real RAM before the memory access actually finishes. Your programme never sees physical addresses; it only ever deals in virtual ones.
The separation pays off in three ways:
- Isolation — each process gets its own private virtual address space, so a misbehaving (or malicious) programme can't peek at another's memory.
- Simplicity — every programme can be compiled as if it starts at the same address, regardless of where it actually lands in physical RAM.
- Flexibility — the OS is free to shuffle data around in physical memory and the programme is none the wiser.
Virtual memory gives each process its own private address space. The MMU translates virtual addresses to physical addresses in hardware. This provides isolation between processes and lets the OS manage physical RAM flexibly.
Page Tables
Virtual memory chops both virtual and physical memory into fixed-size blocks called pages (4 KB is the typical size). For each process the OS maintains a page table that maps virtual page numbers to physical page numbers, the latter usually called frames.
Whenever the CPU touches a virtual address the MMU walks the page table to find the right physical frame. Walking is expensive, so the CPU keeps a small cache called the Translation Lookaside Buffer (TLB) holding recent translations and skips the walk when it can.
Worked Example: Translating One Address
With 4 KB pages, the bottom 12 bits of an address are the offset within its page (2¹² = 4,096), and everything above them is the page number. In hex, 12 bits is exactly three digits, which makes the split easy to do by eye.
Take the virtual address 0x00403a7c:
- Page number: everything except the last three hex digits,
0x403. - Offset: the last three hex digits,
0xa7c.
The MMU looks up page 0x403 in this process's page table. Suppose the entry says that page lives in physical frame 0x1f2. The physical address is the frame number followed by the unchanged offset:
virtual 0x00403 a7c │ └─ offset copied straight across └─ page 0x403 → frame 0x1f2 (from the page table)physical 0x001f2 a7cOnly the page number gets translated. The offset never changes, which is why things that line up within a page in virtual memory still line up in physical memory.
Now run a second process that uses the very same virtual address 0x00403a7c. Its page table might map page 0x403 to frame 0x8c0, so it reaches physical 0x8c0a7c, a completely different byte of RAM. Two programmes, same address, no collision. That's isolation, done in hardware on every memory access.
Try both halves of the example below. The walker uses one flat table with a 20-bit page number, exactly as in the worked example, and only the entry for page 0x403 is filled in. Step Process A through and watch the offset A7C come out the other side untouched. Run the lookup a second time and the TLB answers without touching the table. Then switch to Process B: same address, a TLB miss, and a different frame.
Same address, two processes
Predict: x86-64 uses 48-bit virtual addresses. With 4 KB pages, how big would a single flat page table be, at 8 bytes per entry?
512 GiB per process. 48 address bits minus 12 offset bits leaves 36 bits of page number, so 2³⁶ entries, about 68.7 billion. At 8 bytes each that's 2³⁹ bytes. No machine could afford that for every process, which is why real page tables are split into a tree of levels where unused branches simply don't exist. Virtual Memory Internals walks through exactly how.
Page Faults
Not every virtual page has to live in physical RAM at all times. When a programme touches a page that's not currently mapped, the MMU raises a page fault and hands control to the OS, which decides what to do.
Most page faults are cheap. The first time you write to memory you got from malloc, the page usually doesn't exist yet; the kernel grabs a free frame, zeroes it, maps it in and returns, all within a microsecond or so. That's a minor fault, and a busy programme takes thousands of them a second without anyone noticing.
The expensive kind is a major fault: the page was swapped out to disk to make room for something hotter, and the OS has to read it back before the programme can carry on. The programme is none the wiser, apart from the wait.
This is why a machine can run programmes whose combined memory footprint dwarfs the physical RAM. The OS juggles pages between RAM and disk as needed. The bill comes due in latency: major faults are absurdly slow compared to normal memory access, since hitting disk can run into millions of nanoseconds. When the system page-faults constantly — a condition called thrashing — performance just falls off a cliff.
And when a programme touches an address that isn't valid at all, say a null pointer or freed memory that's been unmapped, there's no page to fetch. The OS gives up and sends the process a segmentation fault.
Here's the address space of a real process. The program prints the virtual addresses of a function, a global, a heap block and a local variable, splits each into page number and offset just like the worked example, then lists the regions the kernel has mapped, straight from /proc/self/maps.
Each maps line is one region: start and end address, permissions (r, w, x, and p for private), then the file behind it, if there is one. Find the region holding each of your four addresses. The heap and stack usually land somewhere new on every run, thanks to address space layout randomisation (ASLR), yet the heap block's offset tends to stay put, because the heap gets shifted in whole pages. If main and global don't move at all, the program was linked at a fixed address rather than built to be relocatable. Every one of these numbers is virtual; your program never learns which physical frames sit behind them.