Skip to main content

Page Replacement Algorithms

Managing memory efficiently is crucial for operating systems, especially when running multiple processes simultaneously. Page replacement algorithms play a significant role in deciding which memory pages to swap out when a page of memory needs to be allocated but all frames are occupied. Let's dive into different page replacement algorithms to understand how they work, their pros and cons, and when to use them.

What is a Page Replacement Algorithm?​

A page replacement algorithm helps in managing the pages in memory, specifically when a page needs to be loaded into RAM, but there is no space available. It decides which page in memory should be replaced with the new one. Effective page replacement strategies ensure optimal system performance by minimizing the number of page faults.

First In, First Out (FIFO) Page Replacement​

FIFO works by maintaining a queue of pages in the order they were loaded. The oldest page is replaced when a new page needs to be brought into memory.

Example​

Consider the following sequence of page references: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2. Assume the main memory can hold only 3 frames.

Page ReferenceFrame 1Frame 2Frame 3Page Fault
77Yes
070Yes
1701Yes
2201Yes
0201No
3231Yes
0230Yes
4430Yes
2420Yes
3423Yes
0023Yes
3023No
2023No
1013Yes
2012Yes

Total Page Faults: 12 out of 15

Advantages​

  • Simple and easy to implement.

  • Requires minimal bookkeeping.

Disadvantages​

  • Often inefficient and can lead to more page faults due to the FIFO nature.

  • Belady's Anomaly: Increasing the number of frames may lead to more page faults, contrary to intuition.

Optimal Page Replacement​

This algorithm replaces the page that will not be used for the longest time in the future. It minimizes page faults and is often used as a benchmark for other algorithms.

Example​

Using the same sequence: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2 with 3 frames.

Page ReferenceFrame 1Frame 2Frame 3Page Fault
77Yes
070Yes
1701Yes
2702Yes
0702No
3302Yes
0302No
4304Yes
2324Yes
3324No
0024Yes
3023Yes
2023No
1013Yes
2012No

Total Page Faults: 10 out of 15

Advantages​

  • Provides the lowest possible page fault rate.

  • Sets a benchmark for other algorithms.

Disadvantages​

Impossible to implement in practice because it requires future knowledge of page references.

Least Recently Used (LRU) Page Replacement​

LRU keeps track of page usage over a short time period. The page that has not been used for the longest time is replaced.

Example​

Using the same sequence: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2 with 3 frames.

Page ReferenceFrame 1Frame 2Frame 3Page Fault
77Yes
070Yes
1701Yes
2201Yes
0201No
3231Yes
0230Yes
4430Yes
2420Yes
3423Yes
0023Yes
3023No
2023No
1013Yes
2012Yes

Total Page Faults: 12 out of 15

Advantages​

  • Provides a good approximation of the optimal page replacement algorithm.

  • Better performance compared to FIFO.

Disadvantages​

  • Requires additional data structures to track page usage.

  • More complex to implement.

Least Frequently Used (LFU) Page Replacement​

LFU keeps a count of how often each page is referenced. The page with the lowest frequency is replaced.

How LFU Works​

  1. Each page in memory has a counter that tracks how many times it has been accessed.

  2. When a page needs to be replaced, LFU chooses the page with the smallest counter.

Example​

Assume pages A, B, C, D have frequencies 1, 3, 2, and 1, respectively, and a new page E needs to be loaded. LFU will choose page A or D since they have the lowest frequency.

Advantages​

  • Effective when the frequency of access is a good predictor of future access patterns.

  • Reduces the likelihood of removing pages that are frequently used.

Disadvantages​

Can suffer from aging problems, where older pages that were used frequently remain in memory, even if they are no longer needed.

Most Frequently Used (MFU) Page Replacement​

MFU is based on the observation that the page with the highest frequency is most likely to have been brought in recently and might be used less in the future.

How MFU Works​

  1. Each page in memory has a counter that tracks its frequency of access.

  2. When a page needs to be replaced, MFU selects the page with the highest counter.

Example​

Assume pages A, B, C, D have frequencies 1, 3, 2, and 4, respectively, and a new page E needs to be loaded. MFU will choose page D since it has the highest frequency.

Advantages​

  • Can be useful in scenarios where high-frequency pages are less likely to be used again soon.

  • Avoids the pitfalls of replacing infrequently used pages.

Disadvantages​

  • May not always accurately predict which pages will be needed.

  • Can lead to inefficiencies in some use cases.