Intermediate

Page Replacement

When memory is full and a new page must come in, which page do you throw out? Eight algorithms, from the simple FIFO baseline through the unbeatable Optimal to the LRU approximations real operating systems actually ship.

osmemorycaching

What is Page Replacement?

The 60-second primer

A page replacement algorithm decides which page to throw out of memory when you need room for a new one. Programs think they have a huge, continuous memory, but physical RAM is much smaller. The operating system keeps the pages a program is actually using in RAM and leaves the rest on disk. When a program touches a page that isn't in RAM — a page fault — the OS must load it. If every RAM frame is already full, something has to go. Choosing what goes is the whole problem.

The choice matters because the page you evict might be needed again in a moment. Guess wrong and you fault it right back in, paying for a slow disk read you could have avoided. Guess well and the pages still in memory are the ones the program keeps using. Every algorithm here is a different rule for making that guess.

The same idea appears in CPU caches, database buffer pools, CDN edge caches, and web caches — anywhere a small fast store fronts a big slow one. The names and units change, but the question never does: the box is full and something new wants in; which old thing do you drop?

Where this shows up

  • Virtual memory — the original setting. The OS pages memory in and out of RAM; a bad policy means thrashing, where the machine spends all its time shuffling pages instead of running your program.
  • Database buffer pools — PostgreSQL, MySQL/InnoDB, and Oracle all keep hot disk pages in RAM and need a replacement policy (often a Clock or LRU variant) to decide what to drop.
  • CPU caches — L1/L2/L3 caches evict cache lines with hardware approximations of LRU; the cost of a miss is a slower memory level, not a disk.
  • Web & CDN caches — Varnish, NGINX, and CDN edge nodes evict objects under memory pressure; LRU and LFU variants are everywhere.
  • Any tiered storage — the moment a fast tier fills, you need a rule for what falls back to the slow tier. The algorithms are the same.

The metric that matters

Almost every policy is judged by one number: the page-fault rate (equivalently, the hit ratio) for a given amount of memory. Fewer faults means less time waiting on the slow tier. The prototypes here report exactly this so you can race the algorithms against the same reference string.

Side-by-side

How they compare

The same concepts, on the same axes. Use this as a map; the individual pages are the territory.

01FIFO
Core rule
Evict the oldest-loaded page
Fault rate
Poor — ignores usage
Cost / metadata
A queue; almost nothing
Best known for
The simplest baseline (and Belady's anomaly)
02Optimal
Core rule
Evict the page used furthest in the future
Fault rate
Best possible (lower bound)
Cost / metadata
Needs the future — unimplementable
Best known for
The yardstick every real policy is measured against
03LRU
Core rule
Evict the least-recently-used page
Fault rate
Excellent in practice
Cost / metadata
Recency order; expensive to track exactly
Best known for
The gold-standard practical policy
04Second Chance
Core rule
FIFO, but spare a page whose reference bit is set
Fault rate
Better than FIFO
Cost / metadata
FIFO queue + 1 bit per page
Best known for
A cheap, easy upgrade over FIFO
05Clock
Core rule
Second Chance as a circular buffer with a hand
Fault rate
Same as Second Chance, far cheaper
Cost / metadata
Ring + 1 bit per page; O(1) work
Best known for
The LRU approximation real kernels actually ship
06NRU
Core rule
Pick from the lowest (R, M) class
Fault rate
Coarse but cheap
Cost / metadata
2 bits per page + periodic tick
Best known for
Respecting dirty pages with minimal bookkeeping
07Aging
Core rule
Shift-register counters approximate recency
Fault rate
Very close to LRU
Cost / metadata
An n-bit counter per page
Best known for
Software LRU approximation in fixed memory
08LFU
Core rule
Evict the least-frequently-used page
Fault rate
Great for stable hot sets, bad for shifting ones
Cost / metadata
A counter per page
Best known for
Workloads with a steady, popular core

Decision guide

Which one should you use?

A practical tour of when each algorithm wins.

How to pick

  • Just measuring the best you could doOptimal. It can't be built (it needs the future), but it tells you how close any real policy gets.
  • You want the best realistic qualityLRU. Recency is a strong signal. Exact LRU is costly, which is why the next few exist.
  • You need LRU's quality but cheaplyClock (or Aging in software). Clock is what most operating systems and database buffer pools actually run.
  • You're starting from FIFO and want an easy winSecond Chance. One reference bit turns FIFO into something respectable; Clock is the same idea done efficiently.
  • Eviction cost depends on whether a page is dirtyNRU. It prefers evicting clean pages so it doesn't have to write them back to disk.
  • Your hot set is stable and popularity is the real signalLFU. But watch for stale, once-popular pages that never leave — frequency without aging gets stuck in the past.

It's one idea, refined

Start at FIFO (age only). Optimal shows the ceiling. LRU uses recency to get near it. Second Chance and Clock approximate LRU with a single bit; NRU adds the dirty bit; Aging adds more bits for finer recency; LFU swaps recency for frequency. Learn them in that order and each one is just the previous with one problem fixed.

Concepts in this track

8 concepts, in order

Each links to a concept page with its own explanation, prototype, and quiz.

Related tracks

If this one clicks, try these next.