Page Replacement Algorithms: FIFO, LRU and Optimal with Page Fault Counting
A page replacement question gives you a reference string (the sequence of pages a process touches), a number of frames (physical memory slots) and an algorithm, usually FIFO, LRU or Optimal. You trace the frames reference by reference and count the page faults, the references that find their page missing from memory. Operating Systems carries 5% of the NSCT according to HEC's Areas of Competencies, and it had the lowest accuracy of any area in 2026, at 33.8%, per the HEC NSCT report and APP.
Page replacement belongs to virtual memory, covered in Memory Management. This page stays numerical: one string traced under all three algorithms, then the anomaly and the timing formula that examiners pair with it.
The method, step by step
- Write the string and the frame count. Frames start empty unless the question says otherwise.
- For each reference, ask: is the page already in a frame? If yes, it is a hit; nothing changes (except LRU's record of last use).
- If not, it is a fault. If a frame is free, load the page there. The first loads into empty frames are faults too; they are called compulsory misses.
- If memory is full, pick a victim by the algorithm's rule: - FIFO: the page that was loaded earliest. - LRU: the page whose last use is oldest (look back). - Optimal (OPT): the page whose next use is farthest in the future, or never comes (look ahead).
- Count faults, then derive what is asked: hit ratio = hits / references, fault rate = faults / references.
The running example
Reference string (13 references), 3 frames, initially empty:
1, 6, 1, 3, 2, 1, 6, 4, 1, 2, 3, 1, 2

There are 5 distinct pages (1, 2, 3, 4, 6), so every algorithm has at least 5 faults.
FIFO: first in, first out
FIFO keeps a queue in load order. A hit does not move a page in the queue.
| # | Ref | Frames after | Result | Evicted |
|---|---|---|---|---|
| 1 | 1 | 1 | Fault | |
| 2 | 6 | 1, 6 | Fault | |
| 3 | 1 | 1, 6 | Hit | |
| 4 | 3 | 1, 6, 3 | Fault | |
| 5 | 2 | 2, 6, 3 | Fault | 1 |
| 6 | 1 | 2, 1, 3 | Fault | 6 |
| 7 | 6 | 2, 1, 6 | Fault | 3 |
| 8 | 4 | 4, 1, 6 | Fault | 2 |
| 9 | 1 | 4, 1, 6 | Hit | |
| 10 | 2 | 4, 2, 6 | Fault | 1 |
| 11 | 3 | 4, 2, 3 | Fault | 6 |
| 12 | 1 | 1, 2, 3 | Fault | 4 |
| 13 | 2 | 1, 2, 3 | Hit |
FIFO: 10 faults, 3 hits. Notice step 5: page 1 was used at step 3, but FIFO still evicts it because it was loaded first. That is FIFO's weakness.
LRU: least recently used
LRU evicts the page whose most recent use is furthest back. A hit refreshes the page.
| # | Ref | Frames after | Result | Evicted |
|---|---|---|---|---|
| 1-4 | 1, 6, 1, 3 | 1, 6, 3 | F, F, H, F | |
| 5 | 2 | 1, 2, 3 | Fault | 6 (last used at 2) |
| 6 | 1 | 1, 2, 3 | Hit | |
| 7 | 6 | 1, 2, 6 | Fault | 3 (last used at 4) |
| 8 | 4 | 1, 4, 6 | Fault | 2 (last used at 5) |
| 9 | 1 | 1, 4, 6 | Hit | |
| 10 | 2 | 1, 4, 2 | Fault | 6 (last used at 7) |
| 11 | 3 | 1, 3, 2 | Fault | 4 (last used at 8) |
| 12 | 1 | 1, 3, 2 | Hit | |
| 13 | 2 | 1, 3, 2 | Hit |
LRU: 8 faults, 5 hits. Because page 1 keeps being used, LRU never evicts it, which is what saves two faults over FIFO.
Optimal (OPT)
At each fault, look ahead in the string and evict the page needed latest.
| # | Ref | Frames after | Result | Why this victim |
|---|---|---|---|---|
| 1-4 | 1, 6, 1, 3 | 1, 6, 3 | F, F, H, F | |
| 5 | 2 | 1, 6, 2 | Fault | next uses: 1 at 6, 6 at 7, 3 at 11, so evict 3 |
| 6-7 | 1, 6 | 1, 6, 2 | H, H | |
| 8 | 4 | 1, 4, 2 | Fault | 6 is never used again |
| 9-10 | 1, 2 | 1, 4, 2 | H, H | |
| 11 | 3 | 1, 3, 2 | Fault | 4 is never used again |
| 12-13 | 1, 2 | 1, 3, 2 | H, H |
OPT: 6 faults, 7 hits. No algorithm can do better on this string with 3 frames, which is why OPT is the yardstick for the others.
| Algorithm | 3 frames | 4 frames |
|---|---|---|
| FIFO | 10 | 6 |
| LRU | 8 | 6 |
| OPT | 6 | 5 |
With 4 frames the hit ratio for OPT rises to 8/13, about 61.5%.
Belady's anomaly
Normally more frames mean fewer faults. Belady's anomaly is the case where FIFO gets more faults with more frames. The textbook string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5:
| Frames | FIFO faults | LRU faults |
|---|---|---|
| 3 | 9 | 10 |
| 4 | 10 | 8 |
FIFO rises from 9 to 10. LRU and OPT are stack algorithms: the pages they hold with n frames are always a subset of those they hold with n + 1 frames, so they can never show the anomaly. Silberschatz and Stallings both use this subset property to explain it.
Page faults and effective access time
A fault is extremely expensive because the page must be read from disk. With fault rate p:
EAT = (1 - p) x memory access time + p x page-fault service time
Worked example: memory access 150 ns, fault service 6 ms (6,000,000 ns), one fault every 4,000 accesses (p = 0.00025).
EAT = 0.99975 x 150 + 0.00025 x 6,000,000 = 149.96 + 1,500 = about 1,650 ns, roughly 11 times slower than memory with no faults. This is why good replacement matters. Address translation costs on a fault-free access (the TLB and page table) are covered in Paging and Address Translation.
Speed tricks
- Lower bound first: faults are at least the number of distinct pages. If frames are at least the number of distinct pages, the answer is exactly that number for every algorithm.
- OPT is always lowest. Any option where OPT exceeds FIFO or LRU is wrong.
- For LRU, scan backwards from the current reference: the last of the frame pages you meet is the victim.
- For OPT, scan forwards: the frame page you meet last (or never) is the victim.
- Cyclic strings: if a loop touches k + 1 pages with k frames, FIFO and LRU fault on every reference.
Common mistakes
- Forgetting to count the faults that fill empty frames.
- Moving a page to the back of the FIFO queue on a hit; that turns FIFO into LRU.
- Using load time instead of last use for LRU.
- Answering "more frames always means fewer faults" without checking for FIFO.
- Mixing up hit ratio and fault rate; they add up to 100%.
- Assuming LRU always beats FIFO. It usually does, but some strings favour FIFO.
Practice set
String A: 7, 2, 7, 3, 1, 2, 4, 7, 3.
- FIFO, 3 frames, string A. Faults?
- LRU, 3 frames, string A. Faults?
- OPT, 3 frames, string A. Faults?
- LRU, 4 frames, string A. Faults?
- String 2, 5, 2, 6, 1, 5, 2, 6 with 3 frames: faults under FIFO and under LRU?
- String 1, 2, 3, 1, 4, 5, 2, 1, 3 frames. When 4 arrives, which page do FIFO, LRU and OPT each evict?
- 40 references cause 10 faults. Hit ratio?
- 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 with 4 frames: LRU faults? OPT faults?
- Memory 150 ns, fault 6 ms, 1 fault in 4,000 accesses. EAT?
Show answers
- 7. Hits at the second 7 and at 2 (step 6).
- 8. Only the second 7 is a hit.
- 6. Hits at steps 3, 6 and 8; at step 5 OPT evicts 3, whose next use is last.
- 7. LRU still loses 7 before it returns.
- FIFO 5, LRU 7. A case where LRU is worse.
- FIFO evicts 1, LRU evicts 2, OPT evicts 3.
- 75%. 30 hits out of 40.
- LRU 10, OPT 6. The cycle defeats LRU.
- About 1,650 ns. See the worked example.
Practise
The quiz below has 20 page replacement questions, filterable by tier and difficulty. Every fault count was produced by a simulator, not by hand.
Practise: 20 questions
FAQs
Why is LRU not used directly in real operating systems?
Exact LRU needs a timestamp or a reordered stack on every memory reference, which is too costly without special hardware. Real systems approximate it with a reference bit, as in the second-chance (clock) algorithm, which gives a recently used page one more pass before evicting it.
Is a page hit counted when the page is loaded for the first time?
No. The first reference to any page is always a fault, even when a frame is free, because the page is not yet in memory. These compulsory misses are why the fault count can never be below the number of distinct pages.
How do I break ties in Optimal replacement?
A tie happens when two or more pages are never used again. Any of them is a correct choice and the fault count is the same; textbooks usually evict the one loaded earliest. Frame contents may differ, so follow the question's rule if it gives one.
Does Belady's anomaly mean FIFO is always worse with more frames?
No. On most strings FIFO improves with more frames, as the running example shows (10 faults with 3 frames, 6 with 4). The anomaly only says it is not guaranteed to improve, unlike LRU and OPT.
Next step
Read the concepts in Memory Management, then work through paging and address translation. Go back to the Operating Systems MCQ hub or sit the timed NSCT mock test.