Page Replacement Algorithms: FIFO, LRU, Optimal, Clock and Belady's Anomaly
How operating systems choose which page to evict, how to trace FIFO, LRU, Optimal and Clock on a reference string and why FIFO shows Belady's anomaly.

Page Replacement Algorithms: FIFO, LRU, Optimal, Clock and Belady's Anomaly
Virtual memory lets programs use more memory than physically exists by keeping only some pages in RAM. When a program needs a page that is not there, a page fault occurs, and if every frame is full the operating system must choose a page to remove. That choice is the page-replacement algorithm. This guide shows how to trace the four classic algorithms by hand and what Belady's anomaly is, with the free page replacement simulator to check your grids.
Setting up the grid
You are given a reference string, the sequence of pages a program uses, and a number of frames. Draw a grid with one column per reference and one row per frame. For each reference:
- If the page is already in a frame, it is a hit: copy the column unchanged.
- If not, it is a fault: load the page into an empty frame, or evict a victim chosen by the algorithm.
Count the faults at the end. The first time each page appears always causes a fault while frames are empty.
FIFO: first in, first out
Evict the page that was loaded earliest. Keep a queue of load order beside the grid.
With the textbook string 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 and three frames, FIFO causes 15 faults. Its weakness: it may throw out a page used constantly just because it arrived early.
LRU: least recently used
Evict the page whose last use was furthest in the past. Look back along the string from the current position; the page you reach last is the victim.
The same string with three frames gives 12 faults. LRU uses the recent past to predict the near future, which works well because programs tend to reuse the same pages (locality of reference).
Optimal (OPT, MIN)
Evict the page that will not be used for the longest time in the future. Look forward along the string; the page you reach last, or never, is the victim.
The same string gives 9 faults, the fewest possible. Real systems cannot see the future, so OPT is a yardstick: an algorithm close to OPT is doing well.
Clock (second chance)
Real hardware cannot afford to track exact LRU order, so systems approximate it. In Clock, frames form a circle and each page has a use bit, set to 1 whenever it is referenced. On a fault, the hand moves around the circle:
- bit 1: clear it to 0 and move on (the page gets a second chance);
- bit 0: evict this page.
The simulator shows the use bits as small numbers in the grid.
Belady's anomaly
Common sense says more frames should mean fewer faults. In 1969 László Bélády and colleagues showed that FIFO can break this rule. With the reference string 1 2 3 4 1 2 5 1 2 3 4 5:
- 3 frames: 9 faults
- 4 frames: 10 faults
Adding memory made performance worse. The simulator's Belady tab plots faults against frames for all four algorithms and points out any increase.
LRU and OPT never show the anomaly because they are stack algorithms: the set of pages kept with n frames is always contained in the set kept with n + 1 frames.
Reading the results
- Hit ratio = hits ÷ references. With 20 references and 12 faults, it is 8 ÷ 20 = 40 %.
- Fault ratio = faults ÷ references.
In real systems a fault costs millions of times more than a memory access, so even a small change in fault rate matters.
Tips for exam grids
- Write the victim under each fault column; it makes checking much easier.
- For LRU, update the "last used" time on hits as well as faults.
- For OPT, if several pages are never used again, any of them may be chosen; mention your tie rule.
Limits
The simulator models one process with pure demand paging and counts faults only. Real operating systems use variants such as working-set policies, page buffering and dirty-bit preferences, and measure cost in time rather than fault counts.


