Page Replacement Algorithms
FIFO, Optimal, LRU, Clock (second chance) and LFU — how the OS chooses which page to evict, Belady's anomaly, and an interactive simulator.
When a page fault occurs and every frame is in use, the OS must evict a page. A good choice keeps the pages that will be needed soon. The same question, "what to evict?", appears in CPU caches, database buffer pools, CDNs and every application cache, so these algorithms are worth knowing well.
Try it
Step through a reference string with 3 frames, switching between FIFO, LRU and OPT. Watch the fault count.
The algorithms
FIFO
Evict the page that has been in memory longest. Simple (a queue), but it ignores usage: a heavily used page loaded early gets evicted anyway.
Optimal (OPT / Belady's MIN)
Evict the page whose next use is furthest in the future. It gives the fewest possible faults, but needs knowledge of the future, so it's only used as a benchmark to measure other algorithms against.
LRU (least recently used)
Evict the page unused for the longest time — using the past to predict the future (temporal locality). Very good in practice. Exact LRU needs work on every memory access, which hardware can't afford for page tables, so OSes approximate it.
Clock (second chance)
An efficient approximation of LRU using the hardware accessed (reference) bit:
- Look at the page under the clock hand.
- If its reference bit is 1, clear it (a "second chance") and advance the hand.
- If its reference bit is 0, evict that page.
The enhanced clock also considers the dirty bit: prefer evicting clean, unreferenced pages, since dirty ones must be written to disk first. Linux uses a related scheme with active and inactive lists (and, since 6.1, multi-generational LRU).
LFU (least frequently used)
Evict the page with the fewest accesses. It handles "popular forever" items well but adapts poorly: something used heavily an hour ago keeps a high count. Usually combined with aging.
Worked example
Reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 with 3 frames:
| Algorithm | Page faults |
|---|---|
| FIFO | 10 |
| LRU | 9 |
| OPT | 7 |
(Use the simulator above to step through each one.)
Belady's anomaly
You'd expect more frames to mean fewer faults. For FIFO, that's not always true:
Reference string 1 2 3 4 1 2 5 1 2 3 4 5 | 3 frames | 4 frames |
|---|---|---|
| FIFO faults | 9 | 10 |
LRU and OPT are stack algorithms: the set of pages held with n frames is always a subset of the set held with n + 1 frames, so they can never suffer this anomaly.
Beyond the OS: caches everywhere
| Policy | Where it's used |
|---|---|
| LRU | Application caches, Redis allkeys-lru, CPU caches (pseudo-LRU) |
| LFU | Redis allkeys-lfu, CDNs for long-lived popularity |
| ARC (adaptive replacement cache) | ZFS — balances recency and frequency automatically |
| 2Q / LRU-K / clock-pro | Database buffer pools (PostgreSQL uses clock sweep) |
| W-TinyLFU | Caffeine (Java), with a frequency sketch admission filter |
| TTL expiry | Most distributed caches; often combined with LRU |
- On a fault with no free frames, the OS evicts a page; the choice determines the fault rate.
- OPT (evict the page used furthest in the future) is optimal but needs the future — a benchmark only.
- LRU works well; Clock approximates it cheaply using reference bits.
- FIFO can suffer Belady's anomaly; stack algorithms (LRU, OPT) can't.
- The same policies (LRU, LFU, ARC, TinyLFU) govern every cache you build.
QWhy don't operating systems implement exact LRU?medium
Exact LRU needs to update an ordering on every single memory access, which would require hardware support on every load and store. OSes instead sample the hardware reference bit periodically (Clock and its variants), which approximates LRU at very low cost.