Operating Systems Reference/Memory Virtualization

Paging & Page Tables

Fixed-size pages, page tables mapping VPN→PFN, multi-level page tables on x86-64, and page faults — the mechanism behind demand paging and copy-on-write.

3/5Overview: 30m

Paging basics

Physical memory is divided into fixed frames; virtual memory into pages (typically 4 KiB, sometimes 2 MiB/1 GiB huge pages). A page table maps Virtual Page Number (VPN)Physical Frame Number (PFN) plus permission bits (read, write, execute, user/supervisor).

Multi-level page tables (x86-64)

A flat table for 2⁴⁸ bytes would be impossibly large. Multi-level tables allocate inner levels only for used regions — sparse address spaces stay cheap. A page walk traverses 4 levels (PML4 → PDPT → PD → PT) on each TLB miss.

Page fault — not always a crash

Fault typeKernel action
Page not present, on swapRead from swap, resume
Page not present, never touchedAllocate frame (demand zero)
Copy-on-write after forkDuplicate page on write
Invalid accessSIGSEGV to process

Demand paging loads code and data only when accessed. Copy-on-write makes fork cheap — parent and child share read-only pages until one writes.

Huge pages

2 MiB / 1 GiB pages reduce TLB pressure for large, contiguous workloads (databases, JVM heaps). Trade-off: internal fragmentation and harder compaction. hugetlbfs and transparent huge pages (THP) are the two Linux paths — THP can cause latency spikes when collapsing.

Senior-level signal

Memory-mapped databases and JVMs tune huge pages because TLB misses dominate at multi-GB working sets. Before enabling THP globally, measure — the compaction daemon has caused production tail-latency incidents.

Where this goes next

Swapping, Overcommit & OOM covers what happens when there aren't enough physical frames — and why free memory lying around doesn't mean you're safe from the OOM killer.

Further Reading

Hands-On Tasks (Optional)

Low-setup exercises on your local machine. No autograding — the goal is to build intuition, not pass a test.

  • Check your system's page size

    Run `getconf PAGESIZE` (Linux/macOS). Note 4096 bytes on most systems; 16384 on some Apple Silicon configs. Understand why large allocations are rounded up to page boundaries.

    5m