Search

Search papers and lessons

Virtual Memory

Level 2 · 8 min read

Virtual memory gives each program its own address space and maps those addresses to physical memory in fixed-size pages, so programs never see where their data really lives.

On this page

  • Colour = topic mix
  • Systems
  • Sign in to see your progress

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.

  1. Isolation. Without it, one buggy program could overwrite another program's memory or the kernel's.
  2. 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.
  3. 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 PP, a power of two. Common values are P=4096=212P = 4096 = 2^{12} bytes. A virtual address vv splits into two parts. The page number is ⌊v/P⌋\lfloor v / P \rfloor, and the offset is v mod Pv \bmod P. 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 pp to physical frame f(p)f(p), then the physical address is

phys(v)=f ⁣(⌊vP⌋)⋅P+(v mod P)\text{phys}(v) = f\!\left(\left\lfloor \frac{v}{P} \right\rfloor\right) \cdot P + \left(v \bmod P\right)

The page table is the function ff, 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 P=4096P = 4096, there are 232/212=2202^{32}/2^{12} = 2^{20} 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 248/212=2362^{48}/2^{12} = 2^{36} pages. At 8 bytes per entry, the flat table would be 2392^{39} 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

Example
Translating one address by hand

Use P=4096P = 4096. The virtual address is v=10876v = 10876 in decimal, which is 0x2A7C0x2A7C in hex.

  1. Page number: ⌊10876/4096⌋=⌊2.65⌋=2\lfloor 10876 / 4096 \rfloor = \lfloor 2.65 \rfloor = 2.
  2. Offset: 10876−2×4096=10876−8192=268410876 - 2 \times 4096 = 10876 - 8192 = 2684.
  3. Suppose the page table maps page 2 to frame 9. Then the frame base is 9×4096=368649 \times 4096 = 36864.
  4. Physical address: 36864+2684=3954836864 + 2684 = 39548.

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.

Example
What the TLB buys, with illustrative numbers

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.

  1. TLB hit: 1+100=1011 + 100 = 101 ns.
  2. TLB miss: 1+100+100=2011 + 100 + 100 = 201 ns, because the walk costs one RAM access and then the data needs another.
  3. If the hit rate is 98%, the effective access time is 0.98×101+0.02×201=98.98+4.02=103.00.98 \times 101 + 0.02 \times 201 = 98.98 + 4.02 = 103.0 ns.
  4. Without a TLB, every access walks, so the time is 201201 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.

Example
The price of a page fault

A hard disk takes about 10 ms to read a page, and RAM takes about 100 ns per access.

  1. Ratio: 10 ms/100 ns=10−2/10−7=10510 \text{ ms} / 100 \text{ ns} = 10^{-2} / 10^{-7} = 10^{5}.
  2. 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

SituationWhat goes wrongBetter choice
Random access over a huge arrayMany TLB misses, because each access touches a different pageUse huge pages (2 MiB or 1 GiB), so one TLB entry covers more memory
Working set bigger than RAMConstant page faults, called thrashing, and throughput collapsesReduce memory use, or add RAM. Paging harder does not help
Small, short-lived programsPage-table overhead and fault latency matter littleNothing to change. Virtual memory is the default
Real-time or low-latency codeA page fault can stall for millisecondsLock pages in RAM with mlock, or preallocate
GPU codeData that is not resident in GPU memory must be fetched over a much slower linkKeep 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 ⌊5000/4096⌋=1\lfloor 5000 / 4096 \rfloor = 1, and the offset is 5000−4096=9045000 - 4096 = 904. 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 232/212=220=1,048,5762^{32} / 2^{12} = 2^{20} = 1{,}048{,}576 entries. At 4 bytes each, that is 2222^{22} 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 0.02×100=20.02 \times 100 = 2 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