Cache Replacement Algorithms in Memory Management
Cache replacement algorithms decide which cached memory block (or page) to evict when the cache is full and a new block must be brought in. This decision directly affects the cache miss rate and therefore the effective performance of memory systems (e.g., CPU caches, OS page caches, and virtual memory paging). Common algorithms include LRU , FIFO , LFU , Random , and Clock.2
Below is a structured explanation of major replacement policies, their mechanics, properties, trade-offs, and typical approximations used in real systems.
Footnotes
-
Least Frequently Used (LFU) page replacement — Wikipedia - Describes LFU concepts and common implementation concerns. ↩
-
Cache replacement — Wikipedia - Overview of multiple replacement policies including FIFO, LFU, random, and related behavior. ↩
Cache Replacement (LRU, FIFO, OPT) - Core Concepts
Key terms and model (used across algorithms)
To reason about replacement algorithms, systems are often modeled with:
- Cache capacity
- Reference string
- Cache hit and cache miss
- Victim selection
Two important baselines:
- OPT (Belady’s optimal) gives the theoretical minimum possible misses for a given trace.
- Practical algorithms approximate OPT with varying costs.
Footnotes
-
Optimal page replacement algorithm — Wikipedia - Defines OPT/Belady’s optimal and its benchmark role. ↩
1On a miss with full cache: which resident item should be evicted?
Replacement Policy Landscape (from theory to practice)
OPT (Belady)
Theoretical baselineUses future knowledge; lower bound on misses."
Footnotes
-
Optimal page replacement algorithm — Wikipedia - Defines OPT/Belady’s optimal and its benchmark role. ↩
LRU and approximations
Recency-based familyLRU evicts least recently used; approximated by Clock/Second-Chance in hardware/OS."
Footnotes
-
Clock page replacement — Wikipedia - Explains Clock/second-chance approach and reference-bit approximation to LRU. ↩
FIFO
Age-based familyEvicts oldest insertion; can violate desirable properties (e.g., Belady’s anomaly)."
Footnotes
-
Cache replacement — Wikipedia - Overview of multiple replacement policies including FIFO, LFU, random, and related behavior. ↩
LFU
Frequency-based familyEvicts least frequently referenced; needs counters and tie-breaking."
Footnotes
-
Least Frequently Used (LFU) page replacement — Wikipedia - Describes LFU concepts and common implementation concerns. ↩
Random
Low-overhead baselineVery simple victim selection; predictable worst-case guarantees depend on model."
Footnotes
-
Cache replacement — Wikipedia - Overview of multiple replacement policies including FIFO, LFU, random, and related behavior. ↩
1) OPT (Optimal / Belady’s algorithm) — “future knowledge”
OPT (Belady’s optimal) chooses, on each eviction, the resident item whose next access occurs farthest in the future (or is never accessed again). This produces the fewest misses among all possible policies for a fixed reference string.
Why it matters: OPT is a benchmark. Many analyses compare real algorithms to OPT to understand how close they can get.
Footnotes
-
Optimal page replacement algorithm — Wikipedia - Defines OPT/Belady’s optimal and its benchmark role. ↩
2) LRU — evict least recently used
LRU evicts the block with the oldest last access time. Intuition: items used recently are likely to be used again.
A practical hardware view maintains an ordering of “most recently used” items; conceptually, LRU is implemented with:
- A timestamp per item, or
- A stack/list ordered by recent touches.
Property (stack behavior): LRU has strong theoretical structure (often described via a “stack property”), which implies monotonic behavior of miss counts across cache sizes in many standard models.
Cost: exact LRU requires updates on every access (time/memory overhead).
Footnotes
-
Clock page replacement — Wikipedia - Explains Clock/second-chance approach and reference-bit approximation to LRU. ↩
Use LRU as a recency baseline
If you need an algorithm that strongly reflects locality of reference, LRU (or close approximations) are often a good starting point—then evaluate overhead.
3) Clock / Second-Chance — approximate LRU (lower overhead)
Clock approximates LRU using:
- a circular list of cache “frames”
- a reference bit per frame indicating whether the item was accessed recently
- a rotating hand (pointer) that scans frames on eviction
A common “second-chance” form:
- When scanning for a victim, if a frame’s reference bit is set, clear it and give that frame a “second chance”.
- If you find a frame with , evict it.
This mimics LRU behavior but avoids per-access reordering, replacing it with cheaper periodic scanning.
Cost trade-off: update is cheap (set reference bit on access), victim selection cost depends on how many frames have .
Footnotes
-
Clock page replacement — Wikipedia - Explains Clock/second-chance approach and reference-bit approximation to LRU. ↩
4) FIFO — evict the oldest inserted
FIFO evicts the item that entered the cache first, regardless of subsequent accesses.
Pros
- Simplicity: maintain a queue of insertion order.
- Low bookkeeping overhead.
Cons / known phenomenon: FIFO can exhibit Belady’s anomaly, where increasing cache capacity can increase miss count for some reference strings.
Footnotes
-
Cache replacement — Wikipedia - Overview of multiple replacement policies including FIFO, LFU, random, and related behavior. ↩
5) LFU — evict least frequently used
LFU evicts the item with the smallest access count over some window/history.
Mechanics
- Keep a counter per item.
- On hit, increment counter.
- On miss with full cache, evict the minimum counter; tie-breakers can use recency or insertion time.
Cost: counters require updates on every hit; large counters can be expensive, so implementations often:
- use approximate counters,
- apply decay (aging) to prevent “frequency forever” effects.
LFU is designed for workload patterns where frequency locality dominates over recency.
LFU needs aging to avoid stale frequency
Pure LFU can keep ancient “popular” items forever even if the workload shifts. Many real LFU variants add decay/aging.
6) Random — evict a random cache line
Random chooses a random resident item to evict on misses.
Pros
- Minimal metadata: no recency/frequency tracking.
- Often simple to implement and sometimes performs surprisingly well in practice under certain assumptions.
Cons
- No direct use of locality information.
- The miss behavior depends strongly on workload; compared to informed policies, it may be worse in many scenarios.
Comparison of replacement algorithms
The following comparison focuses on conceptual behavior and implementation overhead.
Footnotes
-
Optimal page replacement algorithm — Wikipedia - Defines OPT/Belady’s optimal and its benchmark role. ↩
-
Clock page replacement — Wikipedia - Explains Clock/second-chance approach and reference-bit approximation to LRU. ↩ ↩2
-
Cache replacement — Wikipedia - Overview of multiple replacement policies including FIFO, LFU, random, and related behavior. ↩
-
Least Frequently Used (LFU) page replacement — Wikipedia - Describes LFU concepts and common implementation concerns. ↩
How a cache replacement decision is made (generic pipeline)
- 1Step 1
If the requested block is already resident, return it and record any policy metadata (e.g., update recency bit/counter).
- 2Step 2
If the cache has free frames, insert the requested block without eviction.
- 3Step 3
Invoke the replacement policy to select a victim resident block.
- 4Step 4
Remove the victim, bring in the requested block, and initialize any metadata (e.g., set reference bit).
- 5Step 5
Continue to process the next access in the reference string; the policy’s behavior is determined by its metadata updates.
Frequently misunderstood points
Qualitative overhead vs locality awareness (conceptual)
Higher = more metadata/complexity; locality awareness reflects how strongly the policy uses recency/frequency signals.
Cache Replacement Algorithms (Self-check)
Knowledge Check
Which policy is guaranteed to produce the minimum number of cache misses for a fixed reference string?
Knowledge Check
Which algorithm evicts a randomly chosen resident cache line on a miss?
Knowledge Check
In the generic miss path, a replacement policy is invoked when:
Explore Related Topics
Page Replacement Analysis: FIFO vs LRU for a 4-Frame Reference String
The page‑replacement analysis compares FIFO and LRU for the reference string using four frames, showing that LRU incurs fewer faults.
- FIFO simulation results in page faults.
- LRU simulation results in page faults.
- LRU is superior here because it evicts the least recently used page, better exploiting temporal locality.
- FIFO evicts the oldest loaded page, which can discard recently used pages and lead to higher fault rates.
- This example illustrates why LRU often outperforms FIFO, though FIFO’s simplicity can cause anomalies such as Belady’s.
Page Faults and Page Replacement in Virtual Memory Systems
Swapping in Memory Management: Concept, Mechanisms, and Performance Trade-offs