📄 Page Replacement — LRU, FIFO & Optimal
Run a reference string through FIFO, LRU, Clock and Optimal page-replacement policies, counting page faults. See Bélády's anomaly where adding frames makes FIFO worse, not better.
About this simulation
This simulator drives a fixed set of memory frames through a user-supplied reference string under four classic page-replacement policies — FIFO, LRU, Clock (second chance), and Bélády's Optimal — animating the frame table one reference at a time and colouring each cell red on a fault. A built-in preset reproduces Bélády's anomaly: the counter-intuitive case where giving FIFO more frames produces more page faults, not fewer.
🔬 What it shows
A frame-by-frame simulation of virtual-memory paging: each column is one reference from the string, each row is a physical frame, hits are marked green and faults red, and the evicted slot is highlighted whenever a fault forces a replacement. Page faults, hits and the resulting hit ratio update live as the animation advances.
🎮 How to use
Pick a policy from the dropdown, edit the reference string or click Randomise, and drag the Frames slider to change how many pages fit in memory at once. Press Play to animate step by step or Step to advance one reference, and click the Bélády's anomaly preset button to load the classic string 1 2 3 4 1 2 5 1 2 3 4 5 with FIFO selected — then compare the fault count at 3 frames versus 4.
💡 Did you know?
Bélády's anomaly, discovered by László Bélády in 1969, was a genuine surprise to early operating-system designers who assumed more memory could never make a caching policy perform worse. It is only possible with non-stack algorithms like FIFO; LRU and Optimal are provably immune because the set of pages held with n frames is always a subset of the set held with n+1 frames.
Frequently asked questions
What exactly does this simulator animate?
It takes the reference string you provide — a sequence of page numbers a process accesses over time — and replays it one reference at a time against a fixed number of memory frames, using whichever page-replacement policy you select. Each column in the frame table represents one reference, each row one physical frame, and every step is marked as either a hit (page already resident) or a fault (page had to be loaded, possibly evicting another).
How do FIFO, LRU, Clock and Optimal differ in this simulation?
FIFO evicts whichever page has been resident longest, tracked with a simple rotating pointer. LRU evicts the page least recently referenced, using a timestamp recorded on every hit. Clock approximates LRU cheaply with a circular reference-bit scan that gives recently touched pages a second chance before eviction. Optimal looks ahead in the reference string and evicts the page whose next use is furthest in the future — the provably best possible choice, though it needs knowledge no real system has.
How do I reproduce Bélády's anomaly in the simulator?
Click the "Bélády's anomaly preset" button, which loads the reference string 1 2 3 4 1 2 5 1 2 3 4 5 and selects FIFO. Run it with the Frames slider at 3, note the fault count, then increase Frames to 4 and run again: FIFO produces 10 faults with 4 frames versus 9 with 3, even though it was given more memory. Switching the policy to LRU or Optimal on the same string shows faults only ever decrease or stay flat as frames increase.
Why can only some algorithms suffer Bélády's anomaly?
LRU and Optimal are "stack algorithms": the set of pages they keep resident with n frames is always a subset of what they would keep with n+1 frames, which mathematically guarantees faults never increase when memory grows. FIFO and Clock do not have this stack property — the specific page a FIFO policy chooses to evict can change in ways that are not simply "keep everything from the smaller-frame case plus one more," which opens the door to anomalous behaviour.
What do hit ratio and page fault count actually measure?
Every reference in the string is classified as a hit (page already in a frame) or a fault (page absent, requiring a load and possibly an eviction). The hit ratio is simply hits divided by total references, and it is the standard way operating-systems textbooks compare replacement policies on the same workload: a higher hit ratio for a given amount of memory means fewer expensive disk fetches.
Run a reference string through FIFO, LRU, Clock and Optimal page-replacement policies, counting page faults. See Bélády's anomaly where adding frames makes FIFO worse, not better.
3D · Three.js / WebGL renderer · 60 FPS target · runs fully client-side, no install