Virtual Memory — Demand Paging, Page Replacement
878 words
4 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Virtual Memory — Demand Paging, Page Replacement ## 🎯 Learning Objectives - Explain demand paging and how page faults trigger loading - Trace FIFO, LRU, Optimal, and Clock page replacement algorithms - Identify Belady's Anomaly and explain why it occurs - Calculate effective access time with page faults - Underst...

Virtual Memory — Demand Paging, Page Replacement
🎯 Learning Objectives
- Explain demand paging and how page faults trigger loading
- Trace FIFO, LRU, Optimal, and Clock page replacement algorithms
- Identify Belady's Anomaly and explain why it occurs
- Calculate effective access time with page faults
- Understand thrashing and the working set model
1. Demand Paging
1.1 Intuition
Demand paging loads pages only when they are needed, not before. Like a lazy student who only studies topics the night before an exam — you save time if some pages are never needed.
1.2 Page Fault Handling
(Diagram)
1.3 Effective Access Time (EAT) with Page Faults
\text{EAT} = (1-p) \times \text{memory_access} + p \times (\text{page_fault_time})Where p = page fault rate.
Example: Memory access = 200ns, Page fault time = 8ms (8,000,000ns)
| Page Fault Rate | EAT | Performance Degradation |
|---|---|---|
| 0 (none) | 200 ns | 1x (ideal) |
| 0.001 (1 in 1000) | 200 + 0.001 × 8,000,000 = 8200 ns | 41x slower |
| 0.01 (1 in 100) | 200 + 0.01 × 8,000,000 = 80,200 ns | 401x slower |
Key Insight: Even 1 fault per 1000 accesses slows the system by 41×. Page replacement must be highly optimized.
2. Page Replacement Algorithms
2.1 Common Setup for All Examples
| Page reference string | 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 |
|---|---|
| Frames | 3 |
| Goal | Minimize page faults |
2.2 First-In, First-Out (FIFO)
Replace the page that has been in memory the longest.
(Diagram)
Tracing Table (3 frames):
| Ref | 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | 0 | 3 | 2 | 1 | 2 | 0 | 1 | 7 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| F0 | 7 | 7 | 7 | 2 | 2 | 2 | 2 | 4 | 4 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | 7 | 7 |
| F1 | 0 | 0 | 0 | 0 | 3 | 3 | 3 | 2 | 2 | 2 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | |
| F2 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 1 | ||
| Fault? | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Page faults = 15
Belady's Anomaly: Adding more frames can increase page faults!
| Frames | FIFO Faults |
|---|---|
| 3 | 15 |
| 4 | 16 (more faults with more memory!) |
2.3 Optimal Page Replacement (OPT)
Replace the page that will not be used for the longest time in the future. Provably optimal — used as a benchmark.
Tracing Table (3 frames):
| Ref | 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | 0 | 3 | 2 | 1 | 2 | 0 | 1 | 7 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| F0 | 7 | 7 | 7 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 7 | 7 | 7 | 7 | 7 | 7 | 7 |
| F1 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 4 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| F2 | 1 | 1 | 1 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | ||
| Fault? | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Page faults = 9 (optimal benchmark)
2.4 Least Recently Used (LRU)
Replace the page that has not been used for the longest time. Uses past history as a proxy for future use.
Tracing Table (3 frames):
| Ref | 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | 0 | 3 | 2 | 1 | 2 | 0 | 1 | 7 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| F0 | 7 | 7 | 7 | 2 | 2 | 2 | 2 | 4 | 4 | 4 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| F1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| F2 | 1 | 1 | 1 | 3 | 3 | 3 | 2 | 3 | 3 | 3 | 2 | 2 | 2 | 2 | 2 | 7 | 7 | 7 | ||
| Fault? | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Page faults = 12
2.5 Clock (Second-Chance) Algorithm
An approximation of LRU with lower overhead. Pages have a reference bit set by the MMU on each access. The Clock hand sweeps through frames, giving a "second chance" to pages with ref bit = 1.
(Diagram)
Tracing (3 frames):
| Step | Frames (ref bit) | Hand Position | Victim |
|---|---|---|---|
| 1 | 7(1), -, - | 0 | — |
| 2 | 7(1), 0(1), - | 1 | — |
| 3 | 7(1), 0(1), 1(1) | 2 | — |
| 4 (ref 2) | 7(0), 0(1), 1(1) | 0 | 7 (ref was 0) |
| 5 | 2(1), 0(1), 1(1) | 1 | — |
3. Frame Allocation
3.1 Allocation Strategies
| Strategy | Description | Pros | Cons |
|---|---|---|---|
| Equal | Each process gets same number | Fair | Wastes memory for small processes |
| Proportional | Allocate proportional to process size | Efficient | Complex |
| Priority | Higher priority → more frames | Performance | Can starve low-priority |
3.2 Thrashing
Thrashing occurs when a process doesn't have enough frames, causing constant page faults. The CPU is busy handling page faults instead of executing instructions.
Working Set Model: A process needs its working set (set of pages currently in use) in memory to avoid thrashing.
If total WSS > physical memory → thrashing.
4. Algorithm Comparison
| Algorithm | Implementation Cost | Fault Rate | Belady's Anomaly? | Practical? |
|---|---|---|---|---|
| FIFO | Very low | High | Yes | Rarely |
| Optimal | Impossible (needs future) | Lowest | No | Benchmark only |
| LRU | High (full stack / counters) | Low | No | Hardware-limited |
| Clock | Medium (ref bit) | Medium-Low | No | Most common |
| LFU | Medium (counter) | Medium | No | Can suffer from stale data |
5. 📝 Practice Questions
Q1: For the reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 with 3 frames, compute FIFO page faults.Answer: 9 faults. Steps: 1(✓),2(✓),3(✓),4(replaces 1,✓),1(replaces 2,✓),2(replaces 3,✓),5(replaces 4,✓),1(replaces 1,✓? Actually let me trace...)Proper trace: Frames: [1][2][3]; t=4: [4][2][3] (victim 1); t=1: [4][1][3] (victim 2); t=2: [4][1][2] (victim 3); t=5: [5][1][2] (victim 4); t=1: [5][1][2] (hit); t=2: [5][1][2] (hit); t=3: [5][3][2] (victim 1 → 3); t=4: [5][3][4] (victim 2); t=5: [5][3][4] (hit). Total faults = 9. Q2: What is Belady's Anomaly?Answer: Belady's Anomaly is the counterintuitive observation that for FIFO, increasing the number of frames can increase the page fault rate. For the reference string 1,2,3,4,1,2,5,1,2,3,4,5: 3 frames → 9 faults, 4 frames → 10 faults. OPT and LRU do not exhibit this anomaly. Q3: Why is LRU considered "good but expensive"?Answer: LRU requires tracking the exact order of page accesses, which requires either a hardware stack (expensive to update on every access) or counters/registers for each page. The Clock algorithm approximates LRU with just a reference bit, which is much cheaper. Q4: Calculate EAT for page fault rate 0.0001, memory access 100ns, page fault service time 10ms.Answer: EAT = (1-0.0001)×100 + 0.0001×10,000,000 = 99.99 + 1000 = 1100 ns. The page fault time dominates despite the low rate. Q5: What is thrashing and how does the working set model prevent it?Answer: Thrashing occurs when a process's working set (pages actively used) exceeds its allocated frames, causing constant page faults. The working set model monitors the pages each process accesses within a window Δ, and ensures that each process is allocated enough frames to hold its working set.
6. 🔗 Cross-References
- Week 7 - Memory Management: Page tables, TLB, address translation
- Week 10 - I/O: Swap space management
- BSCS3031 (CSD): Cache memory, temporal/spatial locality Join Discord PreviousMemory ManagementNextFile Systems