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

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:
If the requested page is resident, record a hit and leave all frames unchanged.
If it is absent but an empty frame exists, load it and record a fault.
If it is absent and the frames are full, scan only the suffix to the right of the current request.
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.

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.

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.
Keep learning
Optimal Replacement MCQs: 10 Solved Questions with Step-by-Step Page-Fault Explanations
Solve 10 Optimal Replacement questions from GATE, ISRO, HTET, UPPSC and DSSSB papers, then check each answer against a full three-frame page-fault trace.
Page Replacement Algorithms for GATE: FIFO, LRU and Optimal with Belady's Anomaly Explained
One shared reference string traced fault by fault through FIFO, LRU and Optimal page replacement with 3 frames, then re-run under FIFO with 4 frames to demonstrate Belady's anomaly. Includes the counting rules GATE expects, why LRU beating FIFO is a tendency and not a theorem, and the stack-algorithm property that makes LRU and Optimal immune to the anomaly.
Page Replacement Algorithms MCQs: 12 solved questions (FIFO, LRU, Optimal)
Page replacement MCQs for GATE, UGC NET and DSSSB: FIFO and Belady's anomaly, LRU, Optimal, and page-fault counting numericals, each solved and explained.
LRU Page Replacement: Step-by-Step 3-Frame Example and Exam Traps
Trace every reference through fixed frame slots, learn two reliable recency methods, compare LRU with FIFO and Optimal, and avoid mistakes that alter fault counts.





