← Back to CS Fundamentals

Virtual Memory — Paging, Swap

CS FundamentalsSenior

The Question

What is virtual memory and how do paging and swap work?

What a Strong Answer Covers

  • page tables map virtual → physical
  • "pages = fixed-size
  • "swap = disk
  • "process isolation
Virtual Memory — Paging, Swap diagram

Senior-Level Answer

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.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

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.

3/3 — Strong Answer

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.

Common Mistakes

  • Describing virtual memory as just 'more memory via disk' — missing the address space isolation and security/isolation benefits.
  • Not mentioning the TLB — page table translation on every memory access would be unusably slow without hardware caching of translations.
  • Confusing paging with segmentation — these are distinct memory management mechanisms, though x86 historically used both.
  • Not knowing copy-on-write semantics of fork() — this is a core OS optimization built on the paging mechanism.

Follow-Up Questions

  • What is a TLB and what happens on a TLB miss? — TLB is a hardware cache of virtual-to-physical translations. On a miss, the MMU walks the page table (multiple memory accesses) to find the mapping, then caches it.
  • How does fork() use copy-on-write (CoW)? — fork() maps parent and child to the same physical pages with read-only permissions. On the first write by either party, the MMU triggers a fault and the OS copies the page, then maps each to their own copy.
  • Why do high-performance systems often disable swap? — Swap access is 100,000x+ slower than RAM. Under memory pressure, swap thrashing causes severe latency spikes. It's better to OOM-kill a process predictably than degrade unpredictably.
  • What are huge pages and why do databases like to use them? — 2MB or 1GB pages reduce TLB pressure by covering more memory per entry. Databases with large buffer pools (hundreds of GB) benefit significantly from reduced TLB misses.

Related Questions

  • Process vs Thread
  • Race Condition
  • Deadlock — 4 Conditions, Prevention
  • Thread Safety — Is dict Thread Safe?
  • HashMap Internals

Can You Explain This Cold?

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
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout