Virtual Memory
Wikipedia · Virtual memory · CC BY-SA 4.0
Virtual memory lets a process use more memory than physically exists. Pages live on disk until needed, then get loaded into frames on demand. When physical memory is full, the OS picks a victim page to evict. The choice of victim is the page replacement algorithm.
Demand paging
Pages are not loaded until accessed. The first access triggers a page fault: the OS loads the page from disk into a free frame, updates the page table, and resumes the instruction. Most programs exhibit locality, so page faults are rare after warmup.
Page replacement algorithms
FIFO: evict the oldest page. Simple but suffers Belady's anomaly (more frames can mean more faults). LRU: evict the least recently used page. Good performance but expensive to track precisely. Optimal: evict the page that will not be used for the longest time. Impossible to implement (requires future knowledge) but useful as a benchmark.
Thrashing
When a process has too few frames, it faults on almost every access. The OS spends more time swapping pages than executing instructions. This is thrashing. The fix: give the process more frames, or reduce the degree of multiprogramming.
Working set
The working set is the set of pages a process has referenced in the last N memory accesses. If the OS keeps the working set in memory, page faults stay low. The working set model prevents thrashing by refusing to run more processes than memory can support.
Neighbors
- ⚙ Algorithms Ch.4 — B-trees and page replacement: TLB misses trigger page table walks analogous to B-tree lookups
- 🎰 Probability Ch.11 — Markov chains and LRU: page replacement policies (LRU, Clock) are analyzed as Markov chains over access patterns
- 🔢 Discrete Math Ch.4 — graph theory: the working set model uses a recency graph over page references
Foundations (Wikipedia)