How LRU replaces the least recently used page

Short answer: LRU page replacement evicts the page whose last use is oldest. It is the optimal policy among policies that do not see the future, in the sense of the stack property: a larger memory never causes more misses than a smaller one on the same trace. Implementing an exact timestamp scan is too slow for a hot path. A doubly linked list with a hash map moves a hit to the front in constant time and drops the tail on a miss. The clock algorithm approximates this order with a single reference bit.


What the problem actually asks

When a reference misses and every frame is full, some resident page has to go. The past of the trace is known. The future is not. LRU bets that the page unused for the longest time is the one the program is least likely to touch next. Belady’s optimal policy, which evicts the page whose next use is farthest, needs that future and is a benchmark rather than an implementation.

FIFO can suffer Belady’s anomaly: adding a frame can increase the miss count on some traces. LRU is a stack algorithm and does not. That distinction is the reason to prefer the LRU rule when the anomaly matters.


What the algorithm guarantees

On any trace, the pages resident under LRU with m frames are a subset of those resident with m+1 frames after the same prefix, in the inclusion property that stack algorithms have. Misses therefore do not increase when memory grows. A list-and-map implementation handles a hit or a miss in O(1) time aside from the page move itself. Extra memory is one list node per resident page.

The policy is exact only if every use updates the recency. A hardware reference bit that is sampled coarsely is an approximation. The clock algorithm is the usual approximation and will disagree with exact LRU on some traces.


The working steps

  1. Keep resident pages in a doubly linked list, most recent at the head, and a map from page id to list node.
  2. On a hit, unlink the node and insert it at the head.
  3. On a miss with a free frame, insert the new page at the head.
  4. On a miss with no free frame, evict the tail, which is the least recently used page, and insert the new page at the head.
  5. A use that the hardware only records as a reference bit is not this update. That design is the clock algorithm.

The stack property is about this exact policy. Clock, FIFO, and random replacement do not inherit it from the name LRU. Belady’s optimal policy needs the future trace. LRU is the online policy that uses recency.


Approaches that look simpler and fail

Approach Why it fails when recency is updated only on a miss
Move a page to the front only when it is brought in Hits then leave the order unchanged. A hot page that arrived early can be evicted as if it were cold. Every use has to update recency.
Evict the oldest arrival, which is FIFO, and call it LRU FIFO uses arrival time. LRU uses last-use time. They differ as soon as a resident page is reused.
Add a frame and assume every policy will miss less often FIFO has traces where an extra frame adds misses. LRU’s stack property rules that out. The assumption is not free.
Scan timestamps on every miss and quote O(1) A scan of the frames is linear. The list-and-map update is the constant-time implementation.

A one-bit approximation is the clock algorithm. The offline lower bound is Belady’s farthest-next-use policy. LRU is the exact least-recently-used stack policy.


A working example

This Python fragment moves a page to the front of a doubly linked order on every use and evicts the tail when the cache is full. Capacity 2, requests 1 2 1 3. The eviction is page 2.

from collections import OrderedDict
cache = OrderedDict()
cap = 2
evicted = []
for page in [1, 2, 1, 3]:
    if page in cache:
        cache.move_to_end(page)
    else:
        if len(cache) == cap:
            evicted.append(cache.popitem(last=False)[0])
        cache[page] = True
assert evicted == [2]
print(*evicted)

Where it is used

Redis maxmemory-policy can evict by an approximation of LRU, and database buffer pools have used an LRU list for cached pages. A pure LRU list updates on every hit. The clock algorithm is the approximation operating systems actually scan.


When you can drop this algorithm

Drop an exact list when the hardware gives only a reference bit. Use the clock. Keep exact LRU in a software cache where a hit can unlink a node in constant time.


Why this stays published

We publish this so a search for LRU returns the stack property and the list move that implements a hit in constant time. LRU evicts the least recently used page, never suffers Belady’s anomaly, and updates recency on every use rather than only on admission.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *