When physical RAM is near capacity, the Linux kernel `kswapd` daemon scans pages using an active/inactive LRU list, flushing modified dirty pages to SSD swap partitions. Databases (e.g., LMDB, MongoDB) use mmap() to map multi-gigabyte disk files directly into the virtual address space, letting OS demand paging load disk blocks on demand with zero user-space copying.
Visual representation of control loops, memory layout, and execution flow for Virtual Memory, TLB, Page Faults & Replacement.
When the CPU references a virtual address, the MMU checks the TLB in ~1ns. If TLB Hit, the physical frame is retrieved immediately. If TLB Miss, the MMU walks the page table in RAM (costing 100ns).
If the Page Table Entry valid/invalid bit is 0 (page not in physical RAM), the MMU triggers a Page Fault hardware trap to the OS kernel.
The OS locates the missing page on disk swap. If physical RAM is full, it invokes a Page Replacement Algorithm (e.g., LRU/Clock) to evict a victim page, writing it to disk if dirty.
The OS loads the requested page into the freed RAM frame, sets the valid bit to 1, updates the TLB, and transparently restarts the exact CPU instruction that caused the page fault.
| Feature / Dimension | FIFO (First-In, First-Out) | LRU (Least Recently Used) / Clock |
|---|---|---|
| Eviction Criteria | Evicts the oldest page brought into memory regardless of recent usage. | Evicts the page that has not been referenced for the longest period of time. |
| Belady's Anomaly Susceptibility | YES: Increasing physical memory frames can counter-intuitively INCREASE page faults. | NO: Immune because LRU belongs to the class of mathematical "Stack Algorithms". |
| Implementation Overhead | Very simple: standard FIFO queue of frame pointers. | Requires hardware reference bits (Clock algorithm) or doubly-linked list with hash map. |
| Real-World Performance | Poor: Frequently evicts heavily-used global variables just because they were loaded early. | Excellent: Approximates theoretical Optimal algorithm by exploiting temporal locality. |
Detailed answers, interviewer pro tips, key takeaway summaries, and code examples formulated for technical rounds.
✅ Correction: Optimal replacement requires perfect future knowledge of all upcoming page references. It is impossible to implement in practice and is used purely as a benchmark to compare real algorithms (like LRU/Clock) against.
Virtual Memory decouples logical address space from physical RAM via Demand Paging, TLB caching, and swap space.