Page Replacement Algorithms: FIFO, LRU and Optimal with Page Fault Counting

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. Write the string and the frame count. Frames start empty unless the question says otherwise.
  2. 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).
  3. 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.
  4. 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).
  5. 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

Frame-by-frame grids for FIFO, LRU and Optimal page replacement on the reference string 1 6 1 3 2 1 6 4 1 2 3 1 2 with 3 frames, showing hits and faults.
The page's reference string with 3 frames, simulated for FIFO, LRU and Optimal. Red cells are faults; green are hits.

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

Common mistakes

Practice set

String A: 7, 2, 7, 3, 1, 2, 4, 7, 3.

  1. FIFO, 3 frames, string A. Faults?
  2. LRU, 3 frames, string A. Faults?
  3. OPT, 3 frames, string A. Faults?
  4. LRU, 4 frames, string A. Faults?
  5. String 2, 5, 2, 6, 1, 5, 2, 6 with 3 frames: faults under FIFO and under LRU?
  6. String 1, 2, 3, 1, 4, 5, 2, 1, 3 frames. When 4 arrives, which page do FIFO, LRU and OPT each evict?
  7. 40 references cause 10 faults. Hit ratio?
  8. 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 with 4 frames: LRU faults? OPT faults?
  9. Memory 150 ns, fault 6 ms, 1 fault in 4,000 accesses. EAT?
Show answers
  1. 7. Hits at the second 7 and at 2 (step 6).
  2. 8. Only the second 7 is a hit.
  3. 6. Hits at steps 3, 6 and 8; at step 5 OPT evicts 3, whose next use is last.
  4. 7. LRU still loses 7 before it returns.
  5. FIFO 5, LRU 7. A case where LRU is worse.
  6. FIFO evicts 1, LRU evicts 2, OPT evicts 3.
  7. 75%. 30 hits out of 40.
  8. LRU 10, OPT 6. The cycle defeats LRU.
  9. 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

Interactive quiz loading. The first 10 questions are shown below.

  1. Which page replacement algorithm gives the lowest possible number of page faults for every reference string, but cannot be implemented exactly in a real OS?

    1. LRU
    2. FIFO
    3. Second chance (clock)
    4. Optimal (OPT)
    Show answer

    Answer: D. Optimal (OPT)

    OPT replaces the page whose next use lies farthest in the future, which provably minimises faults. It needs the future reference string, so real systems use it only as a benchmark for measuring other algorithms.

  2. Belady's anomaly, where adding frames increases the number of page faults, can occur with which algorithm?

    1. LRU
    2. Optimal
    3. FIFO
    4. Any algorithm, given the right reference string
    Show answer

    Answer: C. FIFO

    FIFO is not a stack algorithm: the pages it holds with n frames are not always a subset of those it holds with n + 1 frames, so more frames can mean more faults. LRU and OPT are stack algorithms and never show the anomaly.

  3. Why can LRU never suffer from Belady's anomaly?

    1. The pages it keeps with n frames are always a subset of the pages it keeps with n + 1 frames
    2. It always evicts the page that will not be used for the longest time
    3. It uses a reference bit instead of timestamps
    4. It allocates frames to processes in proportion to their size
    Show answer

    Answer: A. The pages it keeps with n frames are always a subset of the pages it keeps with n + 1 frames

    LRU is a stack algorithm: with n + 1 frames it holds the n + 1 most recently used pages, which include the n most recent ones. So any hit with n frames is also a hit with n + 1 frames, and faults can never rise.

  4. A reference string of 15 references uses 6 distinct pages: 3, 1, 4, 1, 5, 2, 6, 5, 3, 5, 2, 6, 4, 3, 1. Memory has 6 frames, initially empty. How many page faults occur under FIFO?

    1. 15
    2. 0
    3. 6
    4. 9
    Show answer

    Answer: C. 6

    With at least as many frames as distinct pages, nothing is ever evicted. Each distinct page faults once when first loaded (compulsory misses), so there are 6 faults whatever the algorithm.

  5. A run makes 40 page references and has 14 page faults. What is the hit ratio?

    1. 35%
    2. 74%
    3. 26%
    4. 65%
    Show answer

    Answer: D. 65%

    Hits = 40 - 14 = 26, so the hit ratio = 26/40 = 65%. The fault rate is 14/40 = 35%; the two always add up to 100%.

  6. Which statement correctly contrasts LRU with Optimal replacement?

    1. LRU looks ahead to the farthest next use; OPT looks back to the least recent use
    2. LRU looks back to the least recent use; OPT looks ahead to the farthest next use
    3. Both evict the page that was loaded earliest
    4. LRU needs the future reference string; OPT does not
    Show answer

    Answer: B. LRU looks back to the least recent use; OPT looks ahead to the farthest next use

    LRU uses the past as an estimate of the future and evicts the page unused for the longest time. OPT uses the actual future and evicts the page whose next reference is farthest away. FIFO is the one that looks at load time.

  7. Using FIFO with 3 frames (initially empty), how many page faults does this reference string cause? 2, 4, 1, 4, 3, 5, 2, 4, 1, 3, 5, 2

    1. 10
    2. 12
    3. 11
    4. 7
    Show answer

    Answer: C. 11

    Only the 4th reference (page 4, loaded two steps earlier) is a hit. Every later page has already been pushed out by the time it returns, so there are 11 faults and 1 hit. Count the three initial loads as faults.

  8. Using LRU with 3 frames (initially empty), how many page faults occur for: 5, 1, 2, 5, 3, 1, 4, 5, 2, 1, 3, 4?

    1. 10
    2. 7
    3. 12
    4. 11
    Show answer

    Answer: D. 11

    LRU evicts the page whose most recent use is oldest. Tracing all 12 references gives 11 faults. FIFO would give 10 and OPT 7.

  9. Using Optimal replacement with 3 frames (initially empty), how many page faults occur for: 6, 2, 3, 6, 1, 2, 4, 6, 3, 2, 1, 4, 6?

    1. 12
    2. 8
    3. 7
    4. 9
    Show answer

    Answer: B. 8

    At each fault, evict the page whose next use is farthest away (or never comes). The trace gives 8 faults; LRU on the same string gives 12.

  10. LRU with 3 frames, string 1, 3, 5, 3, 2, 1, 4, 3. When page 4 is referenced (the 7th reference), which page is evicted?

    1. 1
    2. 5
    3. 3
    4. 2
    Show answer

    Answer: C. 3

    Before reference 7 the frames hold 2, 3 and 1 (5 was evicted when 1 returned). Their last uses were 3 at reference 4, 2 at reference 5 and 1 at reference 6, so 3 is the least recently used page and is evicted.

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.