What is LRU replacement?

A cache replacement policy that evicts the least recently used data. It exploits temporal locality well, but exact tracking is costly with many ways, so approximations are used.

Field: Memory Hierarchy

Japanese name: LRU(Least Recently Used)

Tracking the order and its cost

To implement LRU, the order in which the ways of each set were used is recorded and updated on every access. Two ways need only one bit, but distinguishing every order of N ways requires representing N! states, which takes at least 5 bits for 4 ways and 16 bits for 8 ways. Because the update logic also grows complex, caches with many ways use approximations such as tree pseudo-LRU, which uses N−1 bits arranged as a binary tree. Operating systems approximate LRU for page replacement with the clock algorithm, which uses reference bits.

Access patterns LRU handles badly

LRU relies on temporal locality, the tendency for recently used data to be used again. In a loop that repeatedly reads data just slightly larger than the cache, it evicts exactly the data needed next, so every access misses. A scan that streams through a large amount of data used only once also pushes out frequently used data. To address these weaknesses, policies that insert new data at a position where it is evicted sooner and policies that predict the interval until the next reuse have been proposed.

Comparison with the optimal policy, and where it is used

If future accesses were known, the optimal policy (OPT), which evicts the data whose next use is furthest away, would minimize misses, but it cannot be implemented in practice. LRU is a practical way to approach it and is used in caches, TLBs, operating-system page management and database buffers. LRU is a stack algorithm, so it never suffers from Belady's anomaly, in which a larger capacity produces more misses. In software, combining a hash table with a doubly linked list allows each operation in constant time.

Try it: questions about LRU replacement

A fully associative cache holding 2 lines with LRU replacement starts empty. If blocks are referenced in the order A, B, A, C, B, A, how many misses occur?

  1. A4
  2. B5
  3. C6
Show answer and explanation

Answer: B. 5

Step by step: A (miss, [A]), B (miss, [A,B]), A (hit, A becomes most recent), C (miss, evicts least recent B, [A,C]), B (miss, evicts least recent A, [C,B]), A (miss, evicts least recent C, [B,A]), for 5 misses. Counting 4 comes from wrongly evicting A when C arrives; under LRU, the hit on A made it the most recent. 6 overlooks the hit on the third reference.

Books on Memory Hierarchy Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary