Optimal Page Replacement Algorithm: Worked Example and Exam Traps

Learn OPT from zero with a complete three-frame trace. See exactly why each victim is chosen, how seven faults and six hits arise, and which mistakes distort the answer.

KnowledgeGate Team

Exam prep & CS education

Updated 29 Nov 20265 min read
Optimal Page Replacement Algorithm: Worked Example and Exam Traps

Optimal replacement sounds simple: remove the page needed farthest in the future. On a long reference string, however, it is easy to look in the wrong direction, count a hit as a fault, or evict the wrong page. A disciplined trace separates hits, initial loads and full-frame replacements, so every victim decision can be checked.

What Optimal Page Replacement Actually Means

A page reference string records the order in which a process requests pages. A frame is one physical-memory slot available to hold a page. If the requested page is already in a frame, the request is a page hit. If it is absent and must be loaded, the request causes a page fault.

A replacement decision is required only when a fault occurs and every frame is occupied.

Optimal Page Replacement, also called OPT or MIN, follows one precise rule: among the pages currently resident, replace the page whose next use lies farthest ahead in the remaining reference string. If a resident page never occurs again, assign its next use infinity. It is then an optimal victim.

OPT assumes perfect knowledge of future requests. A general-purpose operating system cannot know that future exactly, so OPT is an offline benchmark rather than a directly implementable policy. It sits within operating-system memory management in CS Fundamentals.

The Four-Step Victim-Selection Method

Use this procedure at every position in the string:

  1. If the requested page is resident, record a hit and leave all frames unchanged.

  2. If it is absent but an empty frame exists, load it and record a fault.

  3. If it is absent and the frames are full, scan only the suffix to the right of the current request.

  4. Write each resident page's next-use position. Evict the page with the farthest next use, treating no future use as infinity.

At step 8, page 4 is about to arrive. Before page 4 arrives, the frames contain [2, 0, 3], and the remaining suffix is [2, 3, 0, 3, 2]. Page 2 next appears at step 9, page 3 at step 10, and page 0 at step 11. Page 0 is farthest away, so it is replaced and the state becomes [2, 4, 3].

If two resident pages never occur again, either can be removed without changing the minimum fault count. State your choice, then preserve it in every later frame state.

Worked Example: 13 References, 3 Frames

Consider the reference string [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2] with three initially empty frames, ordered F1, F2 and F3. Loading a page into an empty frame is still a fault.

Step

Reference

F1

F2

F3

Result

Victim and reason

1

7

7

-

-

Fault

Use empty F1

2

0

7

0

-

Fault

Use empty F2

3

1

7

0

1

Fault

Use empty F3

4

2

2

0

1

Fault

7 and 1 never return; choose 7

5

0

2

0

1

Hit

No replacement

6

3

2

0

3

Fault

1 never returns; evict 1

7

0

2

0

3

Hit

No replacement

8

4

2

4

3

Fault

Next uses: 2 at 9, 3 at 10, 0 at 11; evict 0

9

2

2

4

3

Hit

No replacement

10

3

2

4

3

Hit

No replacement

11

0

2

0

3

Fault

4 never returns; evict 4

12

3

2

0

3

Hit

No replacement

13

2

2

0

3

Hit

No replacement

Faults occur at steps 1, 2, 3, 4, 6, 8 and 11, giving 7 page faults. Hits occur at steps 5, 7, 9, 10, 12 and 13, giving 6 hits. The fault ratio is 7/13 and the hit ratio is 6/13. An answer of 4 faults has forgotten the three initial loads.

Table traces optimal page replacement across three frames, including seven faults, six hits, and a step-eight eviction of page 0.

Why OPT Beats FIFO and LRU on This String

With this same reference string and three frames, FIFO produces 10 faults, LRU produces 9, and OPT produces 7. These totals apply to this worked string only. They are not universal differences between the algorithms.

FIFO removes the page that entered memory earliest. LRU looks backward and removes the page used least recently. OPT looks forward to the farthest next use. With future knowledge, OPT provides a lower-bound benchmark for practical policies on the same input.

The earlier Virtual Memory in Operating Systems guide owns the full FIFO, LRU and OPT comparison on this same string. This focused trace owns the OPT decisions: it writes the future position of every resident page before each eviction. The LRU Page Replacement trace uses the same 13 references to explain backward recency instead. Step 8 is diagnostic: LRU evicts page 2, while OPT evicts page 0.

Traps That Change the Fault Count

The most common errors are procedural rather than conceptual:

  • Looking left instead of right. Past use belongs to LRU. OPT inspects only the unprocessed suffix, and only when replacement is necessary.

  • Counting every reference as a fault. A resident page produces a hit and no frame changes. Test residency before doing anything else.

  • Ignoring initial faults. Filling the first three empty frames creates three faults. Every absent request is a fault, whether or not an eviction occurs.

  • Moving frame columns around. Keep F1, F2 and F3 fixed, replacing the victim in its own column.

  • Mishandling infinity or ties. A page with no future use has next use infinity. At step 4, both 7 and 1 qualify, but this table chooses 7 and carries that state forward.

  • Assuming the string repeats. Do not wrap back to its beginning unless the question explicitly says that requests repeat cyclically.

How Exams Turn the Rule into Questions

Questions ask you to calculate faults or hits, identify a victim, compare policies on identical input, or explain why OPT is a benchmark rather than an online implementation. OPT also has the stack property: increasing the frames cannot produce more faults. FIFO, in contrast, can show Belady's anomaly.

For a 60-second check, circle the request, test hit versus fault, write next-use positions above resident pages, mark infinity for no future occurrence, replace only the farthest page, and tally faults separately. The Page Replacement MCQs: 12 Solved (FIFO, LRU, Optimal) practice set applies the same checklist to victim selection and fault counts.

Diagram shows the step-eight replacement decision: frames contain 2, 0, and 3; page 4 arrives; page 0 is evicted, increasing faults from 5 to 6.

Optimal Replacement Checksum for This Trace

Before accepting 7 faults and 6 hits, verify three invariants:

  • Every one of the 13 references is classified once: 7 faults + 6 hits = 13.

  • A full-frame fault changes exactly one slot; a hit changes none.

  • At step 8, next uses 9, 11 and 10 make page 0 the unique farthest-future victim.

The final frames are [2, 0, 3]. Redraw only the four eviction decisions at steps 4, 6, 8 and 11, then fill the hits around them. Use ZERO TO HERO for a structured route across core CS and Operating Systems.

Go deeper with the full courseOperating System

Keep learning

Discussion