Page Swap Algorthims
All virtual pages cannot be kept in memory. So if the memory is full, one page must be evicted before the next one gets into memory. There are several algorithms to do this effectively to reduce the page fault cost.
Note: Every time a page swap occur, the current page in Memory must be written into hard disk, then the page from disk can be loaded into memory.
FIFO Algorithm
First Page comes into the memory gets evicted first
This is the most efficient and simplest algorithm. but this has a caveat, if a page that came first keep on using, it does not matter it will get evicted eventually.
Even though the Page 0 was used twice, and keep on being used again, it gets evicted
Optimal Algorithm
Remove the page that will not be used for the longest period of time in future
This is the most theoretically optimized limit we can do Page Swaps. But since we cannot know the future, this cannot be implemented. This act as a benchmark baseline for all other real world algorithms

7will not be accessed for the longest time, so evict it1same thing next
LRU Algorithm
Replace page that has not been used in the most amount of time.
Real OSes don't implement exact LRU, because it's too expensive. They approximate it using the hardware's reference bit.
Why exact LRU is impractical
Exact LRU would need an update on every memory access, either a timestamp or a reordering of a list. The hardware doesn't do this.
LRU and OPT are cases of stack algorithms that don’t have Belady’s Anomaly
Second Chance Algorithm
Second chance is FIFO with a safety check. It evicts the oldest page, unless that page was used recently. In that case it gets a "second chance" and stays.
Each page has a reference bit, which the hardware sets to 1 on any access. When the OS needs a victim, it looks at the oldest page (the FIFO head):
- Ref bit = 0: the page hasn't been used since it was last checked, so evict it.
- Ref bit = 1: the page was used recently. The OS clears the bit to 0, moves the page to the back of the queue (as if it had just arrived), and checks the next oldest page.
Who Change the ref bit?
| Who | Action |
|---|---|
| MMU (hardware) | Sets the bit to 1 automatically on every read or write to the page. |
| OS | Clears the bit to 0 during the clock sweep, and reads it to decide on eviction. |
Enhanced Second Chance (with the Modify Bit)
Modify bit - This tells if the actual page in memory is in-sync with the one that was in the hard disk - Hard disk page and memory pages are same - Memory page is different from hard disk one (Modified)
Enhancement comes from, it evict the non-modified pages so the "Write back to disk" do not required
Take ordered pair (reference, modify): • (0, 0) neither recently used not modified – best page to replace • (0, 1) not recently used but modified – not quite as good, must write out before replacement • (1, 0) recently used but clean – probably will be used again soon • (1, 1) recently used and modified – probably will be used again soon and need to write out before replacement