Facebook Pixel

Page Replacement Algorithms

When physical memory fills up and a new page needs to come in, something already sitting in RAM has to leave. But which one?

That decision belongs to the page replacement algorithm. Pick the wrong page to evict and you will be fetching it back from disk almost immediately. Pick well and page faults stay low. Four main algorithms tackle this problem, each with a different strategy.

1. FIFO (First In First Out)

The simplest possible approach. Whatever page arrived in memory first is the first one to go when space is needed. The OS maintains a queue of loaded pages. New pages join the back of the queue. When eviction is needed, the page sitting at the front gets kicked out.

No usage tracking is needed. No timestamps. Just the order of arrival.

text
Example Parameters:
Page reference string: 3, 4, 2, 6, 4, 3, 4, 7, 3, 6
Number of frames: 3
StepReferenceFramesPage FaultEvicted
133Yes (1)-
243, 4Yes (2)-
323, 4, 2Yes (3)-
464, 2, 6Yes (4)3 (oldest)
544, 2, 6No-
632, 6, 3Yes (5)4 (oldest)
746, 3, 4Yes (6)2 (oldest)
873, 4, 7Yes (7)6 (oldest)
933, 4, 7No-
1064, 7, 6Yes (8)3 (oldest)

Total Page Faults = 8 Page Fault Rate = 8/10 = 80% Pages Evicted = 3, 4, 2, 6, 3

The Known Weakness: Belady's Anomaly
You would expect adding more frames to always reduce page faults. Counterintuitively, in certain reference patterns, giving FIFO more frames actually causes MORE faults. No other algorithm has this strange property.

2. LRU (Least Recently Used)

LRU takes a more informed approach. Rather than evicting the oldest arrival, it evicts the page that has gone the longest without being touched. The reasoning is that pages used recently are likely to be needed again soon, so they should stay. Pages that nobody has asked for in a while are the safer bet for removal.

Each page access updates that page's usage timestamp. When eviction time comes, the page with the stalest timestamp loses its frame.

text
Example Parameters:
Page reference string: 2, 5, 4, 2, 7, 5, 3, 5, 7, 4
Number of frames: 3
StepReferenceFramesPage FaultEvicted
122Yes (1)-
252, 5Yes (2)-
342, 5, 4Yes (3)-
422, 5, 4No-
575, 4, 7Yes (4)2 (LRU)
654, 7, 5No-
737, 5, 3Yes (5)4 (LRU)
857, 3, 5No-
973, 5, 7No-
1045, 7, 4Yes (6)3 (LRU)

Total Page Faults = 6 Page Fault Rate = 6/10 = 60% Pages Evicted = 2, 4, 3

Notice step 4: page 2 is referenced again, updating its recency. When eviction is needed at step 5, page 2 escapes because it was just used. Page 5 in step 6 similarly survives because it was recently accessed.

The Trade-off
LRU generally performs well and is widely used in real operating systems. The catch is implementation cost. Tracking the exact recency of every page requires a structure like a linked list or stack that gets updated on every single memory access. At the scale modern CPUs operate, this overhead matters.

3. LFU (Least Frequently Used)

LRU cares about when a page was last used. LFU cares about how often a page has been used overall. Every page carries a counter that increments each time it is accessed.

When a frame must be freed, the page with the lowest access count gets evicted. On a tie, FIFO or LRU ordering breaks the deadlock depending on the implementation.

text
Example Parameters:
Page reference string: 4, 3, 1, 4, 3, 4, 6, 3, 4, 2
Number of frames: 3
StepRefFrequency TableFramesFault
144:14Yes (1)
234:1, 3:14, 3Yes (2)
314:1, 3:1, 1:14, 3, 1Yes (3)
444:2, 3:1, 1:14, 3, 1No
534:2, 3:2, 1:14, 3, 1No
644:3, 3:2, 1:14, 3, 1No
764:3, 3:2, 1:1 → evict 1 (freq=1)4, 3, 6Yes (4)
834:3, 3:3, 6:14, 3, 6No
944:4, 3:3, 6:14, 3, 6No
1024:4, 3:3, 6:1 → evict 6 (freq=1)4, 3, 2Yes (5)

Total Page Faults = 5 Page Fault Rate = 5/10 = 50% Pages Evicted = 1, 6

Pages 4 and 3 accumulate high counts early and hold their frames throughout. Pages that show up infrequently get pushed out when space is needed.

The Weakness: The Aging Problem
LFU can trap a page in memory indefinitely if it happened to be accessed many times early in execution, even if it is never needed again. Conversely, a page that is genuinely useful now but new to memory might have a low count and get evicted prematurely.

4. Optimal Page Replacement

The optimal algorithm is the theoretical gold standard. When a page must be evicted, it looks ahead at the entire future sequence of page references and removes whichever page will not be needed for the longest stretch of time going forward.

By definition no other algorithm can do better on the same reference string with the same number of frames. It produces the absolute minimum number of page faults possible.

text
Example Parameters:
Page reference string: 2, 3, 4, 2, 5, 3, 6, 5, 4, 3, 2, 6
Number of frames: 3
StepRefFramesFaultEvictedReason
122Yes (1)--
232, 3Yes (2)--
342, 3, 4Yes (3)--
422, 3, 4No--
552, 3, 5Yes (4)44 next appears at step 9, latest of the three
632, 3, 5No--
762, 3, 6Yes (5)55 appears at step 8 but won't appear after; 6 needed now
852, 3, 5Yes (6)66 next appears at step 12, latest
942, 4, 5Yes (7)33 next at step 10 but not again; 4 comes sooner
1033, 4, 5Yes (8)22 next at step 11 but 5 appears later; evict 2
1122, 3, 4Yes (9)55 not in future reference
1262, 3, 6Yes (10)44 not in future reference

Total Page Faults = 10 Page Fault Rate = 10/12 = 83%

Even the optimal algorithm cannot avoid cold-start faults when frames are empty. Its advantage over other algorithms grows most visible on longer reference strings with more reuse patterns.

Why it cannot be used in practice
The algorithm requires knowing the complete future sequence of page references before execution begins. In real systems, no one knows what a process will access next. Optimal is used purely as a benchmark to measure how close real algorithms come to the theoretical best.

Comparing All Four Algorithms

Using the same reference string (2, 5, 4, 2, 7, 5, 3, 5, 7, 4) with 3 frames as a baseline:

AlgorithmTotal Page FaultsPage Fault RatePractical?
FIFO880%Yes, very simple
LRU660%Yes, moderately complex
LFU550%Yes, but aging issues
OptimalLowest possibleBest caseNo, future unknown

When to Use Which

  • FIFO works when simplicity is the priority and memory access patterns are unpredictable. Its queue structure is trivial to implement and maintain.
  • LRU is the go-to choice for most general purpose operating systems. It strikes a reasonable balance between performance and implementation effort, and it closely approximates optimal behavior on many real workloads.
  • LFU shines in environments where certain pages are accessed heavily and repeatedly over long periods, like database buffer caches where popular tables stay hot. It struggles with workloads that shift access patterns over time.
  • Optimal serves as a measuring stick. When evaluating a new algorithm or tuning an existing one, comparing its fault rate against optimal on the same reference string tells you exactly how much room for improvement remains.

Summary

Page replacement is one of those decisions that happens thousands of times per second on a busy system, yet most of the time goes completely unnoticed. The goal is always the same: keep the pages most likely to be needed next in the limited frames available, and push out the ones least likely to be touched again soon.

FIFO does this crudely, LRU does it well, LFU does it differently, and Optimal does it perfectly but only in theory.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.