| Page | Frame | Valid Bit | Reference Bit | Dirty Bit |
|---|
An interactive, step-by-step simulator for understanding how modern operating systems manage physical memory through paging, segmentation, and page replacement algorithms.
Virtual memory is a memory management technique that provides processes with the illusion of a large, contiguous address space — far larger than physical RAM. It allows the operating system to efficiently multiplex physical memory among multiple processes and to use secondary storage as an extension of RAM.
Key Insight: A process never directly accesses physical RAM. It works with virtual addresses that the Memory Management Unit (MMU) translates to physical addresses at runtime. This translation is governed by the page table, maintained by the OS kernel.
Virtual memory is built on three fundamental concepts that together manage how data moves between RAM and disk.
Physical and virtual memory are divided into fixed-size blocks called pages (virtual) and frames (physical). The OS maps virtual pages to physical frames non-contiguously, eliminating external fragmentation. When a referenced page is not in RAM, a page fault triggers the OS to load it from disk.
Memory is divided into variable-size logical segments (Text, Data, Stack, Heap), each with its own base and limit registers. Variable-size allocation leads to external fragmentation — unusable holes of free memory — which can be resolved by compaction.
When a page fault occurs and RAM is full, the OS must evict a page. The choice of which page to evict is governed by a replacement algorithm that exploits temporal locality (recently used pages will likely be reused) and spatial locality.
CPU generates a virtual address. MMU checks the page table — the valid bit is 0.
A hardware interrupt fires. Control transfers to the OS page-fault handler.
OS finds a free frame, or selects a victim using the replacement algorithm.
The required page is read from swap space into the selected frame.
Page table entry is updated: frame number written, valid bit set to 1.
The faulting instruction is restarted. The access now succeeds — it's a hit.
Each algorithm makes a different tradeoff between implementation complexity, runtime overhead, and approximation quality.
| Algorithm | Strategy | Complexity | Optimal? | Notes |
|---|---|---|---|---|
| FIFO | Evict the page that has been in memory the longest | O(1) — simple queue | No | Can suffer from Bélády's Anomaly: more frames can increase faults. |
| LRU | Evict the page that was least recently used | O(n) — requires tracking recency | Near-optimal | Strong in practice. Exploits temporal locality. Hardware implementation requires timestamps. |
| OPTIMAL | Evict the page not used for the longest time in future | Requires future knowledge | Yes | Theoretically best possible. Unimplementable in practice; used as a benchmark. |
| 2ND CHANCE | Clock approximation of LRU using a reference bit | O(1) amortized — circular buffer | No (approx.) | Used in real OSes (e.g. Linux). Avoids full LRU overhead with similar performance. |
| Page | Frame | Valid Bit | Reference Bit | Dirty Bit |
|---|