Mental model
Linux abstracts physical RAM by assigning every process an isolated Virtual Address Space. The MMU (Memory Management Unit) translates virtual addresses to physical RAM pages (typically 4KB) using multi-level Page Tables and CPU TLB caches.
Theory
- Page Table: Hierarchical table mapping virtual page numbers (VPN) to physical frame numbers (PFN).
- TLB (Translation Lookaside Buffer): On-CPU hardware cache storing recent virtual-to-physical address translations.
- Minor Page Fault: Requested page is in physical RAM but not mapped in the process page table (e.g. COW allocation). Zero disk I/O.
- Major Page Fault: Requested page is not in physical RAM and must be read from disk (swap space or file system). High I/O latency.
Alternatives and trade-offs
- Standard
read()/write(): Copies data between kernel page cache and user space buffer. mmap()File Mapping: Zero-copy I/O; maps disk files directly into process address space, eliminating user/kernel buffer copying.
Failure modes and misconceptions
- TLB Cache Thrashing: Allocating gigabytes of RAM across millions of 4KB pages overloads CPU TLB cache capacity, causing performance drops. Remedy: Enable Transparent Huge Pages (THP 2MB/1GB pages) for large database engines.
- Ignoring Major Page Fault Spikes: High major page fault rates indicate memory starvation forcing constant disk swap reads, degrading application throughput.
Decision scenario
Use mmap() for high-throughput database storage engines to map data files directly into virtual memory, eliminating user-space buffer copy overhead.
Learning outcomes
- Trace virtual-to-physical address translation via MMU page tables and TLB caches.
- Distinguish zero-disk-I/O minor page faults from disk-bound major page faults.
- Optimize file I/O using
mmap()zero-copy memory mapping.
Trade-offs
Virtual memory provides strict process memory isolation and lazy page allocation, but page faults and TLB misses introduce CPU performance overhead.