What is virtual memory and how do paging and swap work?
Virtual memory is an abstraction that gives each process its own private address space, isolated from other processes and from the physical memory layout. A process sees a contiguous address space starting at 0 (in most architectures), regardless of how physical memory is actually arranged or shared.
Paging implements virtual memory by dividing both virtual and physical memory into fixed-size chunks: virtual pages and physical frames (typically 4KB on x86-64, though huge pages of 2MB or 1GB exist). The CPU's Memory Management Unit (MMU) translates every virtual address to a physical address on each memory access using a page table — a per-process data structure maintained by the OS. The translation uses the top bits of the virtual address as the page number to look up the frame number, then adds the offset within the page.
Page table lookups would be too slow on every memory access, so the MMU includes a Translation Lookaside Buffer (TLB) — a hardware cache of recent virtual-to-physical translations. A TLB hit resolves in ~1 cycle; a TLB miss requires a page table walk (several memory accesses). When the OS switches processes, TLB entries must be flushed or tagged with address space identifiers (ASIDs).
When a process accesses a page that isn't currently mapped to a physical frame — because it was never loaded, or has been evicted — the MMU triggers a page fault. The OS page fault handler either loads the page from disk (demand paging), allocates a new zero-filled page, or handles copy-on-write (fork creates child processes that share parent pages until either writes).
Swap (swap space or page file) extends physical memory to disk. When physical RAM is exhausted, the OS's page replacement algorithm (LRU approximations like clock/second-chance in Linux) selects victim pages to evict to swap. Future accesses page them back in. Swap allows overcommitting memory but at a severe latency cost — disk access is 100,000x+ slower than RAM. Modern production systems often run without swap (Kubernetes pods, high-performance servers) to avoid latency spikes from swap thrashing.
Memory-mapped files use the same paging mechanism: file regions are mapped into the virtual address space and pages are loaded on demand from the file, not RAM. This is how executables are loaded and how databases like SQLite and PostgreSQL read their files efficiently.
Correctly explains virtual address spaces and isolation, describes paging with virtual pages/physical frames, page tables, and the page fault mechanism. Explains swap as disk-backed extension of physical memory.
All of the above plus: explains the TLB and its role in making paging practical, describes demand paging and copy-on-write, discusses page replacement algorithms, and explains why production systems often disable swap and the tradeoff involved.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment