Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Virtual memory explained: address spaces, pages, page tables, the TLB, page faults and swapping, and why every program thinks it has all the RAM.
Quick answer: Virtual memory gives every process its own private, continuous range of memory addresses, regardless of how much physical RAM exists or how it is being used. The CPU and operating system translate each virtual address into a physical address on the fly, using per-process page tables. This lets programs be isolated from each other, lets the system load data only when it is needed, and lets total memory use exceed physical RAM by moving idle pages to disk.
Imagine programs used physical RAM addresses directly. Three problems appear immediately:
Virtual memory solves all three with one level of indirection.
Memory is divided into fixed-size chunks:
The common page size is 4 KB, though systems also support larger "huge pages". An address is split into two parts: the page number and an offset within the page. Translation only changes the page number; the offset stays the same.
Because any virtual page can map to any physical frame, a program's memory can be scattered all over RAM while still looking like one neat continuous block.
Each process has a page table that records, for every virtual page, which physical frame holds it and what may be done with it:
| Entry field | Meaning |
|---|---|
| Frame number | Where the page lives in physical RAM |
| Present bit | Is the page in RAM right now? |
| Read / write / execute bits | What access is allowed |
| User / kernel bit | May ordinary code touch it? |
| Accessed and dirty bits | Has it been used or modified recently? |
A 64-bit address space is vast, so a flat table would be impossibly large. Real systems use multi-level page tables: a tree where whole branches for unused regions simply do not exist. On x86-64, a lookup typically walks four levels.
The hardware component that performs this translation is the memory management unit (MMU), built into the CPU.
Walking a four-level table on every memory access would be painfully slow. So the CPU keeps a small, very fast cache of recent translations called the translation lookaside buffer (TLB).
Programs that touch memory in a compact, predictable pattern get many TLB hits. Programs that jump around huge amounts of memory get more misses. This is one reason huge pages help database servers: fewer, bigger pages mean each TLB entry covers more memory. The TLB is separate from, but works alongside, the CPU cache.
What if a program touches a page that is not currently in RAM? The CPU raises a page fault and hands control to the operating system. Despite the name, most page faults are normal:
This mechanism enables several important tricks:
fork(), parent and child share the same frames, marked read-only. A frame is copied only when one of them writes to it.The Linux memory management concepts guide describes how the kernel implements each of these.
When physical RAM runs low, the operating system picks pages that have not been used recently and frees their frames:
If the program later touches a swapped-out page, a major page fault brings it back. This lets a system run more than fits in RAM, at a price: disk is vastly slower than memory. When the system spends most of its time swapping, performance collapses. That is the subject of why your computer slows down when RAM fills up.
Every process sees roughly the same layout: its code, global data, a heap that grows as it allocates memory, shared libraries, and a stack for each thread. Large stretches in between are unmapped. The kernel is mapped into a protected part of every address space too, so system calls do not need a full address space switch. For the difference between the two main working regions, see stack vs heap.
No. Virtual memory is the whole system of address translation, which is always on. Swap is one feature built on top of it, used only when RAM runs short.
Translation adds a small cost, mostly hidden by the TLB. In return you get isolation, demand loading and sharing, which make the system as a whole faster and far more reliable.
On 64-bit systems, typically 128 TB or more of virtual address space, far beyond installed RAM. The practical limit is physical memory plus swap.
Tools often show virtual size, which counts everything mapped, including files and memory that was reserved but never touched. Resident size (RSS) shows what is actually in RAM.
Virtual memory is one of the most successful ideas in computing: add a translation layer between programs and RAM, and you get protection, flexibility and efficiency all at once. Page tables define the mapping, the TLB makes it fast, and page faults let the operating system fill in memory only when it is truly needed.