# Difference between LRU and LFU Page Replacement Algorithm

In this article, you will learn the difference between the LRU and LFU page replacement algorithms. But before discussing the differences, you need to know about the LRU and LFU page replacement algorithms.

## What is LRU Page Replacement Algorithm?

The LRU stands for the Least Recently Used. It keeps track of page usage in the memory over a short period of time. It works on the concept that pages that have been highly used in the past are likely to be significantly used again in the future. It removes the page that has not been utilized in the memory for the longest time. LRU is the most widely used algorithm because it provides fewer page faults than the other methods.

Example:

Let's take the following reference string to understand the LRU Page Replacement algorithm.

5 0 1 2 0 3 2 0 3 4 1 0 5 0 4 3 2 1 2 0 1

Find the number of page faults when the LRU page replacement policy is used. Also, consider the page frame size to be three.

Solution:

Reference String:

5 0 1 2 0 3 2 0 3 4 1 0 5 0 4 3 2 1 2 0 1

String 5 0 1 2 0 3 2 0 3 4 1 0 5 0 4 3 2 1 2 0 1
Frame 3 1 1 1 3 3 3 3 3 3 0 0 0 0 3 3 3 3 0 0
Frame 2 0 0 0 0 0 0 0 0 0 1 1 1 1 4 4 4 1 1 1 1
Frame 1 5 5 5 2 2 2 2 2 2 4 4 4 5 5 5 5 2 2 2 2 2
Miss/Hit M M M M H M H H H M M M M H M M M M H M H

Total number of reference strings = 21

Total number of page faults or page misses = 14

We know that,

Total number of page hits = Total number of reference strings - Total number of page faults

Total number of page hits = 21 - 14 = 7

Page fault probability = Total number of page faults / Total number of reference strings

Page fault probability = 14/21 = 0.67

Page fault percentage = Total number of page faults / Total number of reference strings * 100

Page fault percentage = 14/21*100 = 67%

Explanation:

1. Firstly, there are three empty frames in the memory. Therefore, when 5, 0, 1 come into the frame, they are allocated to the empty frame in order of their arrival. It occurs page fault because 5, 0, 1 are not present in the memory.
2. When 2 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 5, which is the least recently used page.
3. When 0 comes, it is present in the memory. Therefore, a page hit occurs, and no replacement occurs.
4. When 3 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 1, which is the least recently used page.
5. When 2, 0, 3 comes, it is present in the memory. Therefore, a page hit occurs, and no replacement occurs.
6. When 4 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces page 2, which is the least recently used page.
7. When 1 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 0, which is the least recently used page.
8. When 0 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 3, which is the least recently used page.
9. When 5 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 4, which is the least recently used page.
10. When 0 comes, it is present in the memory. Therefore, a page hit occurs, and no replacement occurs.
11. When 4 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 1, which is the least recently used page.
12. When 3 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 0, which is the least recently used page.
13. When 2 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 5, which is the least recently used page.
14. When 1 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 4, which is the least recently used page.
15. When 2 comes, it is present in the memory. Therefore, a page hit occurs, and no replacement occurs.
16. When 0 comes, it is not present in the memory. Therefore, a page fault occurs, and it replaces the oldest page 3, which is the least recently used page.
17. When 1 comes, it is present in the memory. Therefore, a page hit occurs, and no replacement occurs.

1. LRU doesn't suffer from Belady's Anomaly.
2. The page in the main memory that hasn't been used in the longest will be chosen for replacement.
3. It gives fewer page faults than any other page replacement algorithm. So, LRU is the most commonly utilized method.
4. It is a very effective algorithm.
5. It helps in the full analysis.

1. It isn't to implement because it requires hardware assistance.
2. It is expensive and more complex.
3. It needs an additional data structure.

## What is LFU Page Replacement Algorithm?

The LFU page replacement algorithm stands for the Least Frequently Used. In the LFU page replacement algorithm, the page with the least visits in a given period of time is removed. It replaces the least frequently used pages. If the frequency of pages remains constant, the page that comes first is replaced first.

Example:

Let's take the following reference string to understand the LFU Page Replacement algorithm.

7 0 2 4 3 1 4 7 2 0 4 3 0 3 2 7

Find the number of page faults when the LFU page replacement policy is used. Also, consider the page frame size to be three.

Solution:

Reference String:

7 0 2 4 3 1 4 7 2 0 4 3 0 3 2 7

String 7 0 2 4 3 1 4 7 2 0 4 3 0 3 2 7
Frame 3 2 2 2 1 1 1 2 2 2 3 3 3 3 3
Frame 2 0 0 0 3 3 3 7 7 0 0 0 0 0 2 7
Frame 1 7 7 7 4 4 4 4 4 4 4 4 4 4 4 4 4
Miss/Hit M M M M M M H M M M H M H H M M

Total number of reference strings = 16

Total number of page faults or page misses = 12

We know that,

Total number of page hits = Total number of reference strings - Total number of page faults

Total number of page hits = 16 - 12 = 4

Page fault probability = Total number of page faults / Total number of reference strings

Page fault probability = 12/16 = 0.75

Page fault percentage = Total number of page faults / Total number of reference strings * 100

Page fault percentage = 12/16*100 = 75%

Explanation:

1. Firstly, there are three empty frames in the memory. Therefore, when 7, 0, 2 come into the frame, they are allocated to the empty frame in order of their arrival. It occurs page fault because 7, 0, 2 are not present in the memory.
2. When 4 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 7, which is the least frequently used page.
3. When 3 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 0, which is the least frequently used page.
4. When 1 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 2, which is the least frequently used page.
5. When 4 comes, it is present in the memory. Therefore, a page hit occurs, and no replacement occurs.
6. When 7 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 3, which is the least frequently used page.
7. When 2 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 1, which is the least frequently used page.
8. When 0 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 7, which is the least frequently used page.
9. When 4 comes, it is present in the memory. Therefore, a page hit occurs, and no replacement occurs.
10. When 3 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 2, which is the least frequently used page.
11. When 0, 3 comes, it is present in the memory. Therefore, a page hit occurs, and no replacement occurs.
12. When 2 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 0, which is the least frequently used page.
13. When 7 comes, it is not present in the memory. Therefore, a page fault occurs, replacing 2, which is the least frequently used page.

## Main Differences between the LRU and LFU Page Replacement Algorithm

Here, you will learn the main differences between the LRU and LFU Page Replacement Algorithm. Various differences between the LRU and LFU Page Replacement Algorithm are as follows:

1. LRU stands for the Least Recently Used page replacement algorithm. In contrast, LFU stands for the Least Frequently Used page replacement algorithm.
2. The LRU page replacement algorithm keeps track of page usage in the memory over a short time period. In contrast, In the LFU page replacement algorithm, the page with the least visits in a given period of time is removed.
3. LRU removes the page that has not been utilized in the memory for the longest period of time. In contrast, LFU replaces the least frequently used pages.