Page Replacement Algorithms (FIFO · OPT · LRU · LFU)
A page replacement algorithm is the rule that decides which of the already-loaded pages to evict when memory is full and a new page needs to be loaded. The better the choice, the fewer page faults occur and the faster the program runs.
At a Glance
- Analogy: you can only keep 3 books open on your desk (memory), but you need to read a new one. One book has to go back to the stacks (disk). Whether you send back "the book taken out first," "the book you'll need furthest in the future," "the book you haven't looked at for the longest time," or "the book you've looked at the least" is exactly the difference between the algorithms.
- The page evicted at this point is called the victim page.
- Paging, virtual memory, and the steps of page replacement are covered in Memory Management.
Why Page Replacement Is Needed
Virtual memory uses Demand Paging: only the pages needed right now are loaded into memory, and the unused parts stay on disk.
But even loading only the needed pages, memory eventually fills up. On top of that, a page that has been loaded may keep occupying space even after it is no longer needed. So when memory is full, an unused page must be evicted (out) and the page needed now brought in (in) in its place.
If possible, it is better to evict an unmodified page. A modified page has to have its changed contents written back to disk when evicted, which takes more time. (The modify bit used to check this is explained in Memory Management.)
Page Reference String
The CPU requests memory by logical address, but memory is loaded page by page. So if addresses in the same page are requested one after another, only the first one matters; for the rest, the page is already loaded, so a page fault cannot happen.
That is why, when comparing algorithms, instead of addresses we write only the page numbers in order, collapsing consecutive repeats of the same number into one. This is called the page reference string.
Requested address (page size 100) : 0100 0432 0101 0612 0102 0103 0104 0611
Page number : 1 4 1 6 1 1 1 6
Reference string (repeats merged) : 1 4 1 6 1 6Every example in this article is compared using the reference string below and 3 frames. It is a common example in operating systems textbooks.
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1Simulator Code
The tables below were not drawn by hand; they are the results of actually running this Python simulator.
def simulate(refs, n_frames, policy):
frames = [] # pages currently loaded in memory
loaded_at = {} # when each page was loaded into memory (for FIFO)
last_used = {} # when each page was last used (for LRU)
use_count = {} # how many times each page has been used so far (for LFU)
faults = 0
log = []
for t, page in enumerate(refs):
victim = None
if page in frames:
result = "Hit"
else:
result = "Fault"
faults += 1
if len(frames) == n_frames: # if there is no free slot, evict one
if policy == "FIFO": # the page that came in first
victim = min(frames, key=lambda p: loaded_at[p])
elif policy == "OPT": # the page that will be used furthest in the future
def next_use(p):
rest = refs[t + 1:]
return rest.index(p) if p in rest else float("inf")
victim = max(frames, key=next_use)
elif policy == "LRU": # the page used longest ago
victim = min(frames, key=lambda p: last_used[p])
elif policy == "LFU": # the least used page (if tied, the one that came in first)
victim = min(frames, key=lambda p: (use_count[p], loaded_at[p]))
elif policy == "MFU": # the most used page (if tied, the one that came in first)
victim = min(frames, key=lambda p: (-use_count[p], loaded_at[p]))
frames[frames.index(victim)] = page
del use_count[victim]
else:
frames.append(page)
loaded_at[page] = t
use_count[page] = 0
last_used[page] = t
use_count[page] += 1
log.append((t + 1, page, list(frames), result, victim))
return faults, log
refs = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1]
if __name__ == "__main__":
for policy in ["FIFO", "OPT", "LRU", "LFU", "MFU"]:
faults, log = simulate(refs, 3, policy)
print(f"{policy}: {faults} page faults")FIFO: 15 page faults
OPT: 9 page faults
LRU: 12 page faults
LFU: 13 page faults
MFU: 15 page faultsThe "Frames" column in the tables lists the pages in the 3 frames in slot order, and - means an empty slot. For the first three requests, memory is empty, so every algorithm has a fault no matter what.
1. FIFO (First In First Out)
The page loaded into memory first is evicted first. It works like a queue.
- It is the simplest, so it is easy to implement.
- It suits initialization code well. Initialization code is needed only once when the process first runs and is not used after that. Since it is definitely needed at the start, it is loaded first, and FIFO evicts what was loaded first, so it gets cleaned up naturally once initialization is over.
- However, "came first" has nothing to do with "won't be used in the future." A page that was loaded early but is used continuously (e.g. a frequently called function) also gets evicted when its turn comes, causing another fault right away.
| Step | Request | Frames | Result | Evicted page |
|---|---|---|---|---|
| 1 | 7 | 7 - - | Fault | |
| 2 | 0 | 7 0 - | Fault | |
| 3 | 1 | 7 0 1 | Fault | |
| 4 | 2 | 2 0 1 | Fault | 7 |
| 5 | 0 | 2 0 1 | Hit | |
| 6 | 3 | 2 3 1 | Fault | 0 |
| 7 | 0 | 2 3 0 | Fault | 1 |
| 8 | 4 | 4 3 0 | Fault | 2 |
| 9 | 2 | 4 2 0 | Fault | 3 |
| 10 | 3 | 4 2 3 | Fault | 0 |
| 11 | 0 | 0 2 3 | Fault | 4 |
| 12 | 3 | 0 2 3 | Hit | |
| 13 | 2 | 0 2 3 | Hit | |
| 14 | 1 | 0 1 3 | Fault | 2 |
| 15 | 2 | 0 1 2 | Fault | 3 |
| 16 | 0 | 0 1 2 | Hit | |
| 17 | 1 | 0 1 2 | Hit | |
| 18 | 7 | 7 1 2 | Fault | 0 |
| 19 | 0 | 7 0 2 | Fault | 1 |
| 20 | 1 | 7 0 1 | Fault | 2 |
15 page faults. You can see waste like this several times: at step 6, page 0, which was just used at step 5, is evicted, and then it has to be loaded again at the very next step 7.
Here is the same process as a figure. Blue cells are newly loaded pages, and steps that were hits with no change are omitted from the figure.

Image source: gyoogle tech interview notes, Page Replacement Algorithm
Belady's Anomaly
Common sense says that adding frames should reduce page faults. But with FIFO, there are cases where adding frames actually increases faults.
With the simulator above saved as paging.py, frames 3 and 4 were compared on a different reference string.
from paging import simulate
refs = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
for n in (3, 4):
print(f"FIFO, {n} frames: {simulate(refs, n, 'FIFO')[0]} page faults")FIFO, 3 frames: 9 page faults
FIFO, 4 frames: 10 page faultsThis does not happen with OPT or LRU.
2. OPT (Optimal Replacement)
The page that will not be used for the longest time in the future is evicted. If there is a page that will never be used again, it is the first choice.
- In theory, it is the algorithm with the fewest page faults. It can cut faults dramatically compared with FIFO.
- In practice, however, it cannot be used, because the operating system cannot know in advance which pages a program will request.
- So OPT is used as a benchmark for measuring how good other algorithms are, in comparisons like "how close does LRU get to OPT?"
| Step | Request | Frames | Result | Evicted page |
|---|---|---|---|---|
| 1 | 7 | 7 - - | Fault | |
| 2 | 0 | 7 0 - | Fault | |
| 3 | 1 | 7 0 1 | Fault | |
| 4 | 2 | 2 0 1 | Fault | 7 |
| 5 | 0 | 2 0 1 | Hit | |
| 6 | 3 | 2 0 3 | Fault | 1 |
| 7 | 0 | 2 0 3 | Hit | |
| 8 | 4 | 2 4 3 | Fault | 0 |
| 9 | 2 | 2 4 3 | Hit | |
| 10 | 3 | 2 4 3 | Hit | |
| 11 | 0 | 2 0 3 | Fault | 4 |
| 12 | 3 | 2 0 3 | Hit | |
| 13 | 2 | 2 0 3 | Hit | |
| 14 | 1 | 2 0 1 | Fault | 3 |
| 15 | 2 | 2 0 1 | Hit | |
| 16 | 0 | 2 0 1 | Hit | |
| 17 | 1 | 2 0 1 | Hit | |
| 18 | 7 | 7 0 1 | Fault | 2 |
| 19 | 0 | 7 0 1 | Hit | |
| 20 | 1 | 7 0 1 | Hit |
9 page faults. With the same reference string and number of frames, you cannot do better than this. For example, at step 6 when page 3 comes in, of 2, 0, and 1, the one used furthest in the future, page 1 (step 14), is evicted.
In the figure, there are noticeably fewer fault steps than with FIFO.

Image source: gyoogle tech interview notes, Page Replacement Algorithm
3. LRU (Least Recently Used)
The page that has gone unused for the longest time is evicted. It comes from the idea that "what hasn't been used recently won't be used in the future either."
- OPT looks at the future, but LRU decides by looking at the past, so it can actually be used.
- Real programs strongly tend to reuse recently used data soon (temporal locality), so a page not used recently is likely not to be used in the future either.
- It may cause more faults than OPT, but it is one of the best methods among algorithms that can actually be used.
| Step | Request | Frames | Result | Evicted page |
|---|---|---|---|---|
| 1 | 7 | 7 - - | Fault | |
| 2 | 0 | 7 0 - | Fault | |
| 3 | 1 | 7 0 1 | Fault | |
| 4 | 2 | 2 0 1 | Fault | 7 |
| 5 | 0 | 2 0 1 | Hit | |
| 6 | 3 | 2 0 3 | Fault | 1 |
| 7 | 0 | 2 0 3 | Hit | |
| 8 | 4 | 4 0 3 | Fault | 2 |
| 9 | 2 | 4 0 2 | Fault | 3 |
| 10 | 3 | 4 3 2 | Fault | 0 |
| 11 | 0 | 0 3 2 | Fault | 4 |
| 12 | 3 | 0 3 2 | Hit | |
| 13 | 2 | 0 3 2 | Hit | |
| 14 | 1 | 1 3 2 | Fault | 0 |
| 15 | 2 | 1 3 2 | Hit | |
| 16 | 0 | 1 0 2 | Fault | 3 |
| 17 | 1 | 1 0 2 | Hit | |
| 18 | 7 | 1 0 7 | Fault | 2 |
| 19 | 0 | 1 0 7 | Hit | |
| 20 | 1 | 1 0 7 | Hit |
12 page faults. That is 3 fewer than FIFO (15) and fairly close to the optimum (9).
Here it is as a figure. Each cell in the figure corresponds one-to-one with a fault step in the table above.

Image source: gyoogle tech interview notes, Page Replacement Algorithm
LRU is used heavily not only in page replacement but also in caches. How LRU is implemented in a cache like Redis is covered in LRU and TTL.
4. LFU (Least Frequently Used)
The page used the fewest times so far is evicted. Pages used often are considered important and kept. When use counts are tied, the page that came in first was evicted. (This simulator resets the count to 0 when a page leaves memory.)
| Step | Request | Frames | Result | Evicted page |
|---|---|---|---|---|
| 1 | 7 | 7 - - | Fault | |
| 2 | 0 | 7 0 - | Fault | |
| 3 | 1 | 7 0 1 | Fault | |
| 4 | 2 | 2 0 1 | Fault | 7 |
| 5 | 0 | 2 0 1 | Hit | |
| 6 | 3 | 2 0 3 | Fault | 1 |
| 7 | 0 | 2 0 3 | Hit | |
| 8 | 4 | 4 0 3 | Fault | 2 |
| 9 | 2 | 4 0 2 | Fault | 3 |
| 10 | 3 | 3 0 2 | Fault | 4 |
| 11 | 0 | 3 0 2 | Hit | |
| 12 | 3 | 3 0 2 | Hit | |
| 13 | 2 | 3 0 2 | Hit | |
| 14 | 1 | 3 0 1 | Fault | 2 |
| 15 | 2 | 3 0 2 | Fault | 1 |
| 16 | 0 | 3 0 2 | Hit | |
| 17 | 1 | 3 0 1 | Fault | 2 |
| 18 | 7 | 3 0 7 | Fault | 1 |
| 19 | 0 | 3 0 7 | Hit | |
| 20 | 1 | 3 0 1 | Fault | 7 |
13 page faults. LFU's weakness shows here too. Page 3 is used at steps 10 and 12, bringing its count to 2, and from step 13 on it is never used again, yet it holds its spot to the end. Because of that, pages 1, 2, and 7 keep pushing each other out in the second half, and faults continue. This is the problem of a page that was used a lot in the past hanging on.
5. Other Algorithms
MFU (Most Frequently Used)
The opposite of LFU: the most used page is evicted. The idea is that "a page used a lot has already done its job, while a page used little has only just been loaded and will be used from now on." On the reference string above it had 15 faults, the same as FIFO, and in practice it is rarely used.
Clock (Second Chance)
LRU is good, but accurately recording "when it was used" every time a page is used puts a heavy load on hardware. So real operating systems often use the clock algorithm, which roughly imitates LRU.
- Each page has one reference bit. When a page is used, the hardware sets it to 1.
- The pages are arranged in a circle like a clock face, and a hand sweeps around to find a page to evict.
- If the page the hand points at has a reference bit of 1, it thinks "this was used recently," gives it a pass once by setting the bit to 0, and moves on. If the bit is 0, that page is evicted.
┌──── Page A (1) ────┐
│ │
Page D (0) Page B (1) the hand points at B
│ │ → B is 1, so set it to 0 and move on
└──── Page C (0) ────┘ → C is 0, so C is evictedRunning the same reference string with clock gave 14 page faults (with newly loaded pages starting with a reference bit of 1). A variant that looks at both the reference bit and the modify bit is called NUR (Not Used Recently).
Comparing the Results
| Algorithm | Page evicted | Page faults (reference string above, 3 frames) | Real-world use |
|---|---|---|---|
| OPT | The one used furthest in the future | 9 | Impossible (requires knowing the future), used as a benchmark |
| LRU | The one unused for the longest time | 12 | Widely used (mostly as an approximation) |
| LFU | The least used one | 13 | Used in some caches |
| Clock | One whose reference bit is 0 | 14 | Widely used in real operating systems |
| FIFO | The one that came in first | 15 | Simple but performs poorly |
| MFU | The most used one | 15 | Almost never used |
Replacement Scope: Global vs. Local Replacement
With multiprogramming, pages from multiple processes are loaded into memory together. So when choosing a victim, you also have to decide how far to look for candidates.
| Approach | Candidates | Pros | Cons |
|---|---|---|---|
| Global replacement | Pages of all processes in memory | A process that needs memory can take more, so overall throughput is high | My pages can be pushed out because of other processes, so performance can be erratic |
| Local replacement | Only pages of its own process | Not affected by other processes, so performance is predictable | Even if one process has spare frames, other processes can't use them |
In practice, global replacement is more efficient, so general-purpose operating systems like Linux mainly use it. With local replacement, the number of frames each process gets is fixed, so even if some processes have frames to spare and others are short, they cannot share.
Thrashing
If there are too few frames compared with the pages processes are using, pages are needed again right after being evicted, over and over without end. The CPU sits almost idle waiting on the disk, and the system does nothing but page replacement. This is called thrashing.
- Analogy: your desk is so small that as soon as you open a book, you have to return it to the stacks to make room for another, and then fetch it again, over and over.
- Solution: reduce the number of processes running at once, or guarantee each process enough frames to hold the set of pages it has been using frequently (its working set). In the end, adding more memory is the surest fix.
Summary
| Concept | Summary for non-CS majors |
|---|---|
| Page replacement | Deciding which book to send back to the stacks when the desk is full |
| Victim page | The page chosen to be evicted from memory |
| Page reference string | The requested page numbers written in order (consecutive repeats merged into one) |
| FIFO | Evicts the page that came first. Simple, but also evicts frequently used pages |
| Belady's anomaly | With FIFO, adding frames can increase faults |
| OPT | Evicts the page needed furthest in the future. Best possible, but can't be used in practice because the future is unknown |
| LRU | Evicts the page unused for the longest time. Among the best of those actually used |
| LFU | Evicts the least used page. Has the problem of pages used heavily in the past hanging on |
| Clock | An LRU imitation that gives one pass via a reference bit. Widely used by real operating systems |
| Global / local replacement | Whether to choose from all processes or only from your own process |
| Thrashing | A state where frames are so short that the system does nothing but page replacement |
