TL;DR
Virtual memory gives every program its own private range of addresses. The operating system and the hardware translate each address into a place in physical RAM, and they do it in fixed-size blocks called pages. A program can use more memory than the machine has, and two programs can each use "address 0x1000" without touching each other.
Where you'll see it: every modern operating system, and in GPU and LLM serving systems that manage their own memory as pages.
Intuition
Think of a hotel. Each guest is told "your room is number 4," and the front desk keeps a list that maps each guest's room number to a real room in the building. Two guests can both be "room 4" on different floors. The front desk can also move a guest to a different room while the guest is out, as long as the list is updated. The guest never has to know.
Virtual memory is that front desk. A program works with virtual addresses, numbers in its own private space. A table maps each virtual page to a physical frame in RAM, or records that the page is not in RAM at all. The hardware reads that table on every memory access, so the mapping is always in force.
Why does this exist? Three problems, each solved by the same mechanism.
- Isolation. Without it, one buggy program could overwrite another program's memory or the kernel's.
- Simplicity for programs. A program that starts at address 0 and grows contiguously does not need to know which physical memory is free. The operating system can scatter its pages anywhere.
- Memory larger than RAM. Pages that are not used can be kept on disk and brought back in on demand.
flowchart LR
VA[Virtual address] --> TLB{TLB hit?}
TLB -- yes --> PA[Physical address]
TLB -- no --> PT[Walk page table]
PT -- valid --> PA
PT -- not present --> PF[Page fault: load page from disk]
PF --> PT
PA --> RAM[(RAM)]
The math
Fix a page size , a power of two. Common values are bytes. A virtual address splits into two parts. The page number is , and the offset is . The offset is the same in virtual and physical memory, because pages are moved as whole blocks. Only the page number changes.
If the page table maps virtual page to physical frame , then the physical address is
The page table is the function , stored in memory. Each entry also holds flag bits, such as present, writable and dirty.
Size of the page table
A flat page table has one entry per virtual page. For a 32-bit address space with , there are pages, or about one million entries. At 4 bytes per entry, that is 4 MiB per process, which is acceptable.
For a 64-bit machine that uses 48 bits of virtual address, there are pages. At 8 bytes per entry, the flat table would be bytes, which is 512 GiB. That is far too large for one process, most of which uses a small part of its space. So real systems use a multi-level table: a tree where most branches are absent. x86-64 uses four levels.
Translation cache
Walking several levels of table for every access would be slow. The translation lookaside buffer (TLB) is a small cache of recent translations. A hit skips the walk. A miss pays for the walk and then fills the TLB.
Page faults
If the present bit is clear, the access raises a page fault. The operating system handles it: it finds the page on disk, copies it into a free frame, updates the entry, and restarts the instruction. Most programs never see this, except as a slow access.
Worked examples
Use . The virtual address is in decimal, which is in hex.
- Page number: .
- Offset: .
- Suppose the page table maps page 2 to frame 9. Then the frame base is .
- Physical address: .
The offset came through unchanged, and only the page number was looked up. This is why the hardware can translate without reading the whole table.
Assume a RAM access takes 100 ns, a TLB lookup takes 1 ns, and a page-table walk takes one RAM access, 100 ns. These numbers are illustrative, not measured.
- TLB hit: ns.
- TLB miss: ns, because the walk costs one RAM access and then the data needs another.
- If the hit rate is 98%, the effective access time is ns.
- Without a TLB, every access walks, so the time is ns, about twice as slow.
A 98% hit rate costs about 2% over the all-hit case (101 ns), and the TLB nearly halves the time compared with walking every time. The cache matters more than the exact numbers.
A hard disk takes about 10 ms to read a page, and RAM takes about 100 ns per access.
- Ratio: .
- One page fault costs about the same time as 100,000 RAM accesses.
So a program that faults once every thousand accesses runs about 100 times slower than one that never faults. This is why the operating system tries hard to keep the working set in RAM.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Random access over a huge array | Many TLB misses, because each access touches a different page | Use huge pages (2 MiB or 1 GiB), so one TLB entry covers more memory |
| Working set bigger than RAM | Constant page faults, called thrashing, and throughput collapses | Reduce memory use, or add RAM. Paging harder does not help |
| Small, short-lived programs | Page-table overhead and fault latency matter little | Nothing to change. Virtual memory is the default |
| Real-time or low-latency code | A page fault can stall for milliseconds | Lock pages in RAM with mlock, or preallocate |
| GPU code | Data that is not resident in GPU memory must be fetched over a much slower link | Keep data resident and reuse it on-chip. See GPU Memory Hierarchy |
Paging also has a cost in fragmentation. Pages are fixed size, so the last page of an allocation is often partly unused. The average waste is about half a page per allocation, which is small for large allocations and noticeable for many tiny ones.
History
Virtual memory was first built in the early 1960s. The Atlas computer at the University of Manchester is usually cited for an early version that used a "one-level store" with automatic transfer to a drum. Its aim was to make a machine look bigger than its physical memory to programmers. In the late 1960s, Peter Denning studied program behaviour and proposed the working-set model, which explained why thrashing happens and how to avoid it. Paging later appeared in minicomputers such as the VAX, and then in x86 and ARM processors, which made it the default. Today nearly every general-purpose operating system uses it.
Check yourself
QuestionA virtual address is 5000 and the page size is 4096. What are its page number and offset?Show answer
The page number is , and the offset is . The offset stays the same after translation. Only the page number is replaced by the frame number from the page table.
QuestionA flat page table for a 32-bit address space with 4 KiB pages has how many entries, and how much memory does it take at 4 bytes per entry?Show answer
There are entries. At 4 bytes each, that is bytes, which is 4 MiB per process. This is why flat tables were practical on 32-bit machines, and why 64-bit machines need multi-level tables.
QuestionWhy does a TLB hit rate of 98% leave the effective access time only about 2% above the ideal, when a miss costs a full extra memory access?Show answer
The effective time is the average of the two cases. The ideal is one access, 101 ns in the example. A miss adds one more access, 100 ns, but it happens only 2% of the time. The average extra cost is ns, so the total is about 2% above the ideal.
QuestionA server process has a working set of 12 GB and the machine has 8 GB of RAM. Its throughput collapses under load. What is the most likely cause, and what is the right fix?Show answer
The likely cause is thrashing. The process keeps touching pages that are not in RAM, so most accesses cause page faults. The fix is to reduce the working set or add RAM. Tuning the page replacement policy helps only a little, because the working set simply does not fit.
QuestionWhy do GPU and LLM-serving systems use paging-like schemes even though they could allocate one big buffer?Show answer
A single large buffer wastes memory when requests have very different lengths, because each buffer must be sized for the longest possible sequence. Fixed-size pages let the system allocate memory on demand and share it between requests without copying. Applying the virtual-memory idea to the KV cache, as in Efficient Memory Management for LLM Serving with PagedAttention (vLLM), removes that waste.
Sources
- Virtual memory, Wikipedia
- Arpaci-Dusseau and Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapters on address spaces and paging.