Page Replacement — FIFO, LRU, Optimal & Thrashing
The Problem — A Full Counter
When a page fault hits and every frame is occupied, the OS must evict a victim page to free a frame. A good page-replacement algorithm removes the page least likely to be needed soon — because every wrong choice becomes another slow disk fault.
The Replacement Procedure — Six Steps
Every frame carries a dirty bit. If the victim was only read since it loaded, its disk copy is still valid — the OS can skip the write-back and just overwrite it, turning a two-I/O eviction into one. Clean victims are cheaper to evict, so good algorithms prefer them when they can.
FIFO — Evict the Oldest Arrival
FIFO keeps a queue of arrival order and always evicts the oldest page — cheap to implement, no per-access bookkeeping. But "oldest" isn't "least useful": a heavily-used page loaded early gets evicted anyway. On the string 7 0 1 2 0 3 0 4 2 3 with 3 frames, FIFO takes 9 faults.
Belady's Anomaly — More Frames, More Faults
You'd expect more RAM to only ever help. But with FIFO, giving the process a 4th frame raises the fault count from 9 to 10 on this string. FIFO isn't a "stack algorithm", so its frame sets don't nest as frames grow — the anomaly Belady discovered. LRU and OPT never suffer it.
LRU — Evict the Least Recently Used
LRU evicts the page unused for the longest time, betting that recently-used pages will be used again (locality). It needs per-access bookkeeping — timestamps or a stack — but pays off: on the same string it takes 8 faults, one better than FIFO, and it never hits Belady's anomaly.
Optimal — Evict What's Needed Farthest Away
Optimal evicts the page that will be referenced farthest in the future — provably the fewest faults possible (6 here). It needs to see the future, so it can't be built for real, but it's the yardstick: every practical algorithm is measured by how close it gets to OPT.
Same String, Three Verdicts
On identical input, FIFO is 50% worse than Optimal and LRU is 33% worse. LRU lands closest to the unreachable ideal — which is exactly why real kernels approximate LRU rather than settle for FIFO's simplicity.
Full Trace — 3 Frames
Reference string 7 0 1 2 0 3 0 4 2 3, 3 frames. Frame set after each reference (fault marked ✱).
| Ref | 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | Faults |
|---|---|---|---|---|---|---|---|---|---|---|---|
| FIFO | 7✱ | 0✱ | 1✱ | 2✱ | 0 | 3✱ | 0✱ | 4✱ | 2✱ | 3✱ | 9 |
| LRU | 7✱ | 0✱ | 1✱ | 2✱ | 0 | 3✱ | 0 | 4✱ | 2✱ | 3✱ | 8 |
| OPT | 7✱ | 0✱ | 1✱ | 2✱ | 0 | 3✱ | 0 | 4✱ | 2 | 3 | 6 |
All three fault on the first four cold references and hit on the second 0. They part ways after: LRU saves a fault by keeping 0 resident at ref 7; OPT saves more by foreseeing that 2 and 3 return, keeping them for the final two hits — 9, 8, 6.
LRU Walkthrough — A Second String
The two hits land on the repeated 4s (refs 4 and 7), which LRU keeps resident because they were used recently. Every other reference misses, for 8 faults out of 10. Notice at the last step LRU evicts 4 — now the least-recently-used — to bring in 5.
Thrashing — The Performance Cliff
More processes normally means better CPU use — until their combined working sets no longer fit in RAM. Then every process constantly faults, stealing frames from every other, and the system spends more time paging than computing. CPU utilisation collapses off a cliff — that's thrashing.
The Working-Set Model — Denning
An alternative is Page-Fault Frequency control: set an upper and lower fault-rate threshold. If a process faults too often, give it more frames; if it faults rarely, take some away. Either way the goal is the same — hold each process's resident set near its working set so faults stay rare.
Choosing an Algorithm
| Algorithm | Implementation | Overhead | Faults | Belady's Anomaly |
|---|---|---|---|---|
| FIFO | Queue of arrival order | Very low | Highest (poor) | Yes |
| LRU (true) | Timestamps / stack per access | High | Close to OPT | No (stack algorithm) |
| LRU approx. | Reference bits · clock algorithm | Medium | Good | No |
| Optimal | Requires future knowledge | N/A | Provably minimum | No |
True LRU's per-access bookkeeping is too costly in hardware, and FIFO is too weak (and anomaly-prone). The sweet spot is approximate LRU — the clock (second-chance) algorithm using a reference bit — cheap enough for hardware yet nearly as good as true LRU. OPT stays a benchmark only.
Replacement in the Real World
Eight Rules for Page Replacement
Choosing the Right Victim
From the six-step procedure and the dirty-bit shortcut, through FIFO, LRU and Optimal traced frame-by-frame, Belady's anomaly, thrashing and the working-set model — you can run any replacement algorithm by hand and reason about why a system slows to a crawl.
Page replacement is the last piece of the memory-management arc — from contiguous allocation and paging, through virtual memory and demand paging, to keeping the fault rate low here. Next the course turns to storage and file systems, where these pages finally meet the disk.
🔄 End of tutorial · Press ← to review, or click Restart