# Optimal Page Replacement Policy in Operating System - CodeTextPro

Optimal page replacement policy in an operating system with an example:
Replace the page that will not be used for the longest period of time.
Don’t replace that page which will be used in the near future.

In this post, we will learn how the optimal page replacement policy work.
In an operating system, whenever a new page is referred and not present in memory, a page fault occurs and the Operating System replaces one of the existing pages with the newly needed page. Different page replacement algorithms suggest different ways to decide which page to replace. The target for all algorithms is to reduce the number of page faults. In this algorithm, OS replaces the page that will not be used for the longest period of time in the future.

Optimal page replacement Example-1.
Page reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
Frame size=3

Optimal page replacement Example-2:
Page string: 0, 1, 7, 2, 3, 2, 7, 1, 0, 3

In case of 1:
when insert1, check the previous frame and the existing elements are 2, 3, 7

We see that among three elements (2, 3, and 7) 3 will be used near future.

2 and 7 both are not present in the future.

So here we apply FIFO to break the tie.

Here replace 7 because 7 inserted into the frame before 2.

In case of 0:
when insert0, check the previous frame and existing elements are 2, 3, 1.

We see that among three elements (2, 3, 1) 3 will be used near future.

2 and 1 both are not present in the future.

So apply FIFO to break the tie.

1 will be replaced because between 2 and 1, 1 is inserted before 2.

Optimal page replacement Example-3:

Page reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3