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 type | Kernel action |
|---|---|
| Page not present, on swap | Read from swap, resume |
| Page not present, never touched | Allocate frame (demand zero) |
Copy-on-write after fork | Duplicate page on write |
| Invalid access | SIGSEGV 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 size5m
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.