Why are multi-level page tables used, and how do they save memory?
HardConceptVirtual Memory & Paging
Answer
A single-level page table needs an entry for every page in the address space, even for unused regions, so it becomes very large. A multi-level (hierarchical) page table pages the page table itself: an outer table points to inner page tables, and inner tables exist only for regions the process actually uses.
- Flat table size: a 32-bit address space with 4 KB pages has 2^32 / 2^12 = 2^20 (about one million) pages; at 4 bytes per entry, that is 4 MB per process.
- A two-level split of 10 | 10 | 12 bits: the first 10 bits index the outer table (1,024 entries), the next 10 an inner table, and the last 12 are the offset. Each table is 1,024 × 4 B = 4 KB, exactly one page.
- The cost is time: each level adds a memory access on a TLB miss (four levels on x86-64), which the TLB keeps rare. An inverted page table, with one entry per physical frame, is another way to save space.
Example: a small process uses 4 MB of code and data at the bottom of its address space and 4 MB of stack at the top. Each inner table maps 1,024 × 4 KB = 4 MB, so it needs the outer table plus two inner tables, 3 × 4 KB = 12 KB, instead of 4 MB for a flat table.