Virtual Memory and Paging
Understand virtual and physical addresses, split an address into page number and offset, translate it with a page table, and see why the TLB matters.
Welcome back!
Step 1 of 12Learn
Every process thinks it owns the machine
Run two copies of the same program and print the address of a global variable in each. On many systems both print the same number, say 0x601040. Yet each copy can change its variable without affecting the other. How can two different values live at the same address?
The answer is virtual memory. The addresses a program uses are virtual addresses, which exist only in that process's private address space. The hardware's memory management unit (MMU) translates each virtual address into a physical address, the real location in RAM, on every single memory access, using mapping tables that the kernel maintains for each process.
This indirection gives the OS remarkable powers:
- Isolation: a process can only reach physical memory that its mappings point to. Another process's memory is simply not addressable.
- Simplicity: every program can be compiled as if it had a large, contiguous memory starting at the same place, no matter where its data really sits in RAM.
- Overcommitment: the total virtual memory of all processes can exceed physical RAM, because pages not in use can live on disk.
- Sharing: two processes can map the same physical memory, such as a shared library's code, so it's loaded once.
- Protection: each mapping carries permission bits, so code can be marked read-only and data non-executable.
A 64-bit process today typically has a virtual address space of 128 terabytes or more, far larger than any computer's RAM, of which it uses only a tiny fraction.
Step 2 of 12Practice · Diagram
Every memory access goes through translation
Which step makes the same virtual address lead to different RAM in two processes?
Tap the shape that answers the question, then press Check.
- CPU issues virtual address
For example 0x601040, the same in both copies of the program.
- TLB lookup
A fast cache of recent translations. A hit skips the page table.
- Page table walk (on a miss)
The MMU reads this process's page table in RAM.
- Physical address
Different frames for the two processes.
- RAM
Step 3 of 12Practice · Select all that apply
Which benefits does virtual memory provide? Select all that apply.
Select all that apply.
Step 4 of 12Learn
Pages, frames and the page table
Translating each byte individually would need an impossibly large table. Instead, memory is divided into fixed-size blocks:
- Virtual memory is split into pages.
- Physical memory is split into frames of the same size.
- The common size is 4 KB (4,096 bytes), though systems also support large pages of 2 MB or 1 GB.
Each process has a page table that maps page numbers to frame numbers. Any page can go into any free frame, so a process's memory doesn't need to be contiguous in RAM, which eliminates the fragmentation problems of older schemes.
A virtual address is split into two parts:
- The page number: the high bits, used as an index into the page table.
- The offset: the low bits, the position within the page. It passes through unchanged.
With 4 KB pages, the offset needs 12 bits, because 212 = 4,096. The MMU looks up the page number, finds the frame number, and glues the frame number to the unchanged offset to form the physical address.
Each page table entry (PTE) holds more than a frame number. It has a valid bit (is this page mapped and in RAM?), permission bits (read, write, execute, user or kernel only), a dirty bit set when the page is written, and an accessed bit set when it's used. The kernel relies on those last two when deciding which pages to evict.
Step 5 of 12Note
Step 6 of 12Practice · Work it out
Pages are 4,096 bytes. A process accesses virtual address 9,000, and its page table maps page 2 to frame 6. What is the physical address?
Step 7 of 12Learn
Page table sizes and multi-level tables
Consider a 32-bit virtual address with 4 KB pages. The offset takes 12 bits, leaving 20 bits for the page number, so there are 220 = 1,048,576 possible pages. With 4-byte entries, a flat page table would need 4 MB, per process, even for a tiny program that uses a few pages. A hundred processes would need 400 MB just for page tables.
The fix is a multi-level (hierarchical) page table. Split the page number into pieces. The first piece indexes a top-level table whose entries point to second-level tables, and so on. If a whole region of the address space is unused, its lower-level tables simply don't exist. Since most of a process's address space is empty, this saves huge amounts of memory.
x86-64 processors use four levels, or five on newer systems that support enormous address spaces, translating 48-bit or 57-bit virtual addresses. The downside is speed: a translation may require reading four or five table entries from memory before the real access can happen.
That cost would be crippling if it happened on every load and store, which brings us to the TLB.
Step 8 of 12Chart
Page table memory for one small program (32-bit, 4 KB pages)
Step 9 of 12Practice · Work it out
A system uses 32-bit virtual addresses and 8 KB pages (8,192 bytes). How many bits of each address form the offset?
Step 10 of 12Learn
The TLB: caching translations
The translation lookaside buffer (TLB) is a small, very fast cache inside the MMU that stores recent page-to-frame translations. It typically holds from a few dozen to a couple of thousand entries.
On every memory access, the MMU first checks the TLB:
- TLB hit: the frame number is available almost instantly, and the access proceeds.
- TLB miss: the hardware (or on some architectures, the OS) walks the page table in memory, finds the entry, uses it and stores it in the TLB for next time.
Because programs show strong locality, TLB hit rates are usually above 99%. A single entry covers a whole 4 KB page, so a loop over an array makes hundreds of accesses per TLB entry.
The effective access time combines the two cases. If a memory access costs 100 ns and a miss needs one extra memory access for the table walk, then with a 98% hit rate: 0.98 × 100 + 0.02 × 200 = 98 + 4 = 102 ns, only 2% slower than with no translation at all.
When the OS switches to a different process, the TLB's entries belong to the old address space. Older systems flushed the whole TLB on each process switch, which made context switches expensive. Modern CPUs tag entries with an address-space identifier so entries from several processes can coexist. Large pages help too: one 2 MB page needs a single TLB entry where 4 KB pages would need 512.
Step 11 of 12Try the calculator
Effective access time with a TLB
Assume a TLB miss costs one extra memory access for the page table walk (a simplification).
- Effective access time
- 102 ns
- Slowdown vs. no translation
- 2 %
Step 12 of 12Practice · Choose one
A program's performance improves noticeably after the OS starts backing its large, frequently scanned data array with 2 MB pages instead of 4 KB pages. What is the most likely reason?
Lesson complete!