Facebook Pixel

Optimal Page Replacement

Imagine if an operating system had a time machine. Whenever physical RAM filled up, the OS could simply look into the future, perfectly predict exactly what the program was going to do next, and make the absolute perfect choice of which page to evict.

This is the premise of the Optimal Page Replacement algorithm.

What is the Optimal Algorithm?

Often referred to as Belady's Optimal Algorithm, the core rule is simple: When a page must be evicted, look ahead at the future sequence of memory requests. Evict the page that will not be needed for the longest amount of time in the future.

If a page is never going to be used again, it is the first candidate for eviction.

The Benchmark of Perfection
By definition, no other algorithm can possibly perform better on the same reference string. It produces the absolute mathematically minimum number of page faults possible.

A Worked Example

Let's trace exactly how the Optimal algorithm makes its decisions.

text
Example Parameters:
Page reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3
Number of physical frames: 3
StepRefFramesFaultEvictedReason
111Yes--
221, 2Yes--
331, 2, 3Yes--
441, 2, 4Yes3Future is [1, 2, 5, 1, 2, 3]. 1 and 2 are needed immediately. 3 is needed last. Evict 3.
511, 2, 4No--
621, 2, 4No--
751, 2, 5Yes4Future is [1, 2, 3]. 1 and 2 are needed. 4 is NEVER needed again. Evict 4.
811, 2, 5No--
921, 2, 5No--
1031, 2, 3Yes5Future is empty. OS can evict any page (we evict 5 as it is never used again).

Pros and Cons

AdvantagesDisadvantages
Mathematically Perfect: Guaranteed to yield the absolute lowest possible page fault rate for any given system.Impossible to Implement: An Operating System cannot predict the future. It has no way of knowing what memory addresses a program will request 5 seconds from now.
No Belady's Anomaly: Unlike FIFO, adding more physical frames will ALWAYS reduce or maintain the number of page faults.Not a Real Solution: It cannot be written into the actual code of a live operating system.

Why do we study it if it's impossible?

If it cannot be programmed, why do operating system engineers care about it? Because it serves as the ultimate benchmark.

When engineers design a new, realistic algorithm (like LRU or Second Chance), they need to know how good it actually is. They run a simulation using a massive string of memory requests. If LRU generates 12,000 page faults, is that good or bad?

To find out, they run the exact same simulation using the Optimal algorithm. If Optimal generates 11,500 faults, the engineers know LRU is doing a fantastic job, operating at 95% of theoretical perfection. If Optimal generates only 4,000 faults, they know their LRU implementation needs serious work.

Summary

The Optimal Page Replacement algorithm is a theoretical gold standard. By evicting the page that will not be needed for the longest amount of time in the future, it guarantees the absolute minimum number of page faults. While completely useless for running a live computer, it is an invaluable analytical tool for evaluating the performance of real-world algorithms.

Predicting the Optimal Eviction

Question 1 of 1

Test your ability to trace the Optimal Algorithm's 'look ahead' logic.

Assume physical RAM has 3 frames and they are currently loaded with pages: [A, B, C]. A new request for page 'X' arrives, causing a page fault. The OS looks into the future and sees the upcoming requests will be: B, A, D, C, B. Which page will the Optimal algorithm evict to make room for X?
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.