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.

Intermediate 4 min readLesson 12 of 16 Interview Q&A

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.

InteractivePage replacement — 3 frames
7
7
F
0
7
0
F
1
7
0
1
F
2
2
0
1
F
0
2
0
1
✓
3
2
0
3
F
0
2
0
3
✓
4
4
0
3
F
2
4
0
2
F
3
4
3
2
F
0
0
3
2
F
3
0
3
2
✓
2
0
3
2
✓
Reference 7: page fault (frame free). Faults so far: 1
1/13

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:

A ref=1B ref=0C ref=1D ref=0E ref=1
Pages in a circle; the hand sweeps clockwise.
  1. Look at the page under the clock hand.
  2. If its reference bit is 1, clear it (a "second chance") and advance the hand.
  3. 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:

AlgorithmPage faults
FIFO10
LRU9
OPT7

(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 53 frames4 frames
FIFO faults910

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

PolicyWhere it's used
LRUApplication caches, Redis allkeys-lru, CPU caches (pseudo-LRU)
LFURedis allkeys-lfu, CDNs for long-lived popularity
ARC (adaptive replacement cache)ZFS — balances recency and frequency automatically
2Q / LRU-K / clock-proDatabase buffer pools (PostgreSQL uses clock sweep)
W-TinyLFUCaffeine (Java), with a frequency sketch admission filter
TTL expiryMost distributed caches; often combined with LRU
Key takeaways
  • 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.
Interview questions
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.