1. Home
  2. Operating Systems
  3. Paging & Page Replacement (FIFO, LRU, Optimal)

Paging & Page Replacement (FIFO, LRU, Optimal)

When RAM is full, which page should be thrown out? Watch FIFO, LRU and Optimal handle the same page references — and see Belady's anomaly.

Interactive 3DIntermediate13 min readOSUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

Programs use more pages than fit in RAM. When a needed page is missing, a page fault occurs and the OS must load it — and if RAM is full, choose a victim page to throw out. Which page should go? Press Run.

Pseudocode

  1. for each page reference p:
  2. if p is in a frame: HIT
  3. else PAGE FAULT:
  4. if a frame is free: load p there
  5. else evict the page that was loaded EARLIEST
Read this model as text · 15 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. Programs use more pages than fit in RAM. When a needed page is missing, a page fault occurs and the OS must load it — and if RAM is full, choose a victim page to throw out. Which page should go? Press Run.
  2. Reference 7: not in memory → page fault. A frame is free, so just load it.
  3. Reference 0: not in memory → page fault. A frame is free, so just load it.
  4. Reference 1: not in memory → page fault. A frame is free, so just load it.
  5. Reference 2: not in memory → page fault. Memory is full, so evict 7 — it has been in memory the longest.
  6. Reference 0: already in memory → hit.
  7. Reference 3: not in memory → page fault. Memory is full, so evict 0 — it has been in memory the longest.
  8. Reference 0: not in memory → page fault. Memory is full, so evict 1 — it has been in memory the longest.
  9. Reference 4: not in memory → page fault. Memory is full, so evict 2 — it has been in memory the longest.
  10. Reference 2: not in memory → page fault. Memory is full, so evict 3 — it has been in memory the longest.
  11. Reference 3: not in memory → page fault. Memory is full, so evict 0 — it has been in memory the longest.
  12. Reference 0: not in memory → page fault. Memory is full, so evict 4 — it has been in memory the longest.
  13. Reference 3: already in memory → hit.
  14. Reference 2: already in memory → hit.
  15. FIFO with 3 frames: 10 page faults, 3 hits (hit ratio 23%). Compare with the other algorithms on the same string.

Try this in the 3D model

  • Run FIFO, LRU and Optimal on the textbook string. Count the page faults for each.
  • Choose the Belady's anomaly preset and run FIFO with 3 frames, then 4 frames.
  • Type your own reference string (digits 0–9) and predict the faults before running.

Paging in one minute

Programs see a big, continuous virtual memory. The OS splits it into fixed-size pages (e.g. 4 KB) and RAM into equal-size frames. A page table maps each page to the frame that holds it:

virtual address = page number | offset
physical address = frame number | offset      (frame looked up in the page table)

Not every page needs to be in RAM at once. If a program touches a page that isn’t loaded, the hardware raises a page fault, and the OS loads the page from disk. Disk access is about 100,000× slower than RAM, so we want as few faults as possible.

Page replacement

When RAM is full and a new page is needed, the OS must evict a victim page. The rule it uses is the page replacement algorithm. In the 3D model, the RAM frames are on the left, and the grid shows the frame contents after each reference — H for hit, F for fault.

FIFO — First In, First Out

Evict the page that has been in memory the longest. Easy (just a queue), but it might throw out a page that’s used constantly.

LRU — Least Recently Used

Evict the page that hasn’t been used for the longest time. It relies on temporal locality: recently used pages are likely to be used again soon. Usually much better than FIFO. Real hardware approximates it with “reference bits” (the clock / second-chance algorithm).

Optimal (OPT / Belady’s algorithm)

Evict the page that will be used furthest in the future (or never). It gives the minimum possible number of faults — but needs to know the future, so it’s only a benchmark.

Worked example (3 frames)

Reference string 7 0 1 2 0 3 0 4 2 3 0 3 2:

Algorithm Page faults
FIFO 10
LRU 9
Optimal 7

Run all three in the model to watch where they differ.

Belady’s anomaly

You’d expect more RAM to always help. With FIFO it sometimes doesn’t! For the string 1 2 3 4 1 2 5 1 2 3 4 5, FIFO makes 9 faults with 3 frames but 10 with 4 frames. LRU and Optimal never show this anomaly (they are stack algorithms).

Code

def page_faults(refs, frames, policy="lru"):
    memory, faults, last_used, loaded = [], 0, {}, {}
    for i, p in enumerate(refs):
        if p in memory:
            last_used[p] = i                 # hit
            continue
        faults += 1                          # page fault
        if len(memory) == frames:
            if policy == "fifo":
                victim = min(memory, key=lambda q: loaded[q])
            elif policy == "lru":
                victim = min(memory, key=lambda q: last_used[q])
            else:                            # optimal
                future = refs[i + 1:]
                victim = max(memory, key=lambda q: future.index(q) if q in future else float("inf"))
            memory.remove(victim)
        memory.append(p)
        loaded[p] = last_used[p] = i
    return faults

refs = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
for pol in ("fifo", "lru", "opt"):
    print(pol, page_faults(refs, 3, pol))

Thrashing

If processes don’t have enough frames for their working set (the pages they actively use), almost every reference faults, and the system spends all its time swapping pages instead of doing work — thrashing. Fixes: give processes more frames, run fewer processes at once, or use the working-set model.

Common mistakes

  • Counting the first loads into empty frames as hits — they are faults too (compulsory misses).
  • In LRU, forgetting to update the “last used” time on a hit.
  • Confusing pages (virtual, fixed-size) with segments (variable-size).

Complexity at a glance

Case / operationTimeWhy
FIFO per referenceO(1)A queue of loaded pages.
LRU per referenceO(1)Hash map + doubly linked list.
Optimal per referenceO(n)Must look into the future — not implementable in practice.
Extra spaceO(frames)

Quick check

Test yourself — pick an answer to see if you got it.

1. A page fault happens when…

2. LRU evicts the page that…

3. What is Belady's anomaly?

4. Why can't operating systems use the Optimal algorithm?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Paging & Page Replacement (FIFO, LRU, Optimal). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.