Skip to content

Virtual Memory

Virtual memory gives each process the illusion of having its own large, contiguous memory. The OS maps virtual addresses to physical addresses using a page table. Pages are loaded from disk on demand (demand paging). When RAM is full, the OS uses page replacement (LRU, FIFO, Clock) to swap pages to disk. Thrashing occurs when too many page faults cause the system to spend more time swapping than executing.

Key Concepts

Deep Dive: Paging
Virtual Address Space              Physical Memory (RAM)
┌──────────┐                      ┌──────────┐
│ Page 0   │──→ Page Table ──→    │ Frame 3  │
│ Page 1   │──→               ──→ │ Frame 0  │
│ Page 2   │──→               ──→ │ Frame 7  │
│ Page 3   │    (on disk)         │ Frame 1  │
└──────────┘                      └──────────┘
  • Page: Fixed-size block of virtual memory (typically 4KB)
  • Frame: Same-size block in physical memory
  • Page table: Maps virtual pages to physical frames
  • TLB (Translation Lookaside Buffer): Cache for page table (fast lookup)
Deep Dive: Page Replacement Algorithms
Algorithm Description Performance
FIFO Replace oldest page Simple, Belady's anomaly
LRU Replace least recently used Good, expensive to implement
Clock Circular buffer with use bit Approximate LRU, efficient
Optimal Replace page used farthest in future Best (theoretical only)
Deep Dive: Thrashing

When a process doesn't have enough frames, it generates excessive page faults.

More processes → Less frames each → More page faults → CPU idle → OS adds more processes → Worse!
Solutions: Working set model, page fault frequency, increase RAM.

Common Interview Questions
  • What is virtual memory? Why is it needed?
  • What is a page fault?
  • What is the TLB?
  • Compare page replacement algorithms.
  • What is thrashing? How do you prevent it?
  • What is demand paging?