Learning outcomes
- Analyze virtual memory allocation and page table translation
- Optimize file I/O performance using mmap() memory mapping
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.
// C example: Memory mapping a file directly to virtual address space
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
int main() {
int fd = open("data.bin", O_RDONLY);
size_t length = 4096;
// Map file into virtual address space
char *map = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
if (map != MAP_FAILED) {
printf("First byte: %c\n", map[0]);
munmap(map, length);
}
close(fd);
return 0;
}
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.
Evidence assessment
Theory and decision mastery
Decision scenario
You are designing a high-concurrency production system requiring reliable execution of linux virtual memory paging under heavy traffic load.
Which architectural decision ensures maximum resilience, scalability, and system stability?
Primary sources
- Linux Kernel Kernel.org Official Architecture & Systems Documentation — Linux Kernel Organization, verified 2026-07-23