← 💾 Virtual Memory
Subtopic 2 of 5

6.2Page replacement: FIFO, LRU and Optimal

The idea

When all frames are full a replacement policy picks a victim: FIFO, LRU or Optimal.

Think of it like this: A small desk: when it is full you remove either the paper that has been there longest (FIFO), the one you have not touched for longest (LRU), or the one you will not need for longest (OPT).

Why it exists

The policy decides how many faults you suffer, and each fault costs about a million times a memory access.

How it works

  1. Write the reference string; one column per reference.
  2. Hit: page already in a frame. Fault: bring it in.
  3. If a frame is free use it, otherwise evict by the policy: FIFO oldest loaded, LRU least recently used, OPT farthest next use.
  4. Count the faults.

Worked example: String 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1, 3 frames

FIFO 15 faults, LRU 12, Optimal 9. OPT is the lower bound for any policy.

Now play with it

  • →Switch the policy between FIFO, LRU and OPT and compare the fault counts.
  • →Add more frames and watch faults drop (for these policies).
ref70120304230321201701
frame 177722224440000000777
frame 20000333222221111100
frame 3111100033333222221
fault?FFFFhitFFFFFFhithitFFhithitFFF
Page faults15
Hits5
Hit ratio0.25
Fault rate0.75

Faults with 1 to 7 frames using this policy and string:

1 frame2 frames3 frames4 frames5 frames6 frames7 frames
20151510966

A red number means more frames caused more faults: Belady's anomaly. It can happen with FIFO, never with LRU or OPT (stack algorithms). Try the Belady preset with FIFO.

Check yourself

Which policy has the fewest page faults on any reference string with a given number of frames?

Remember: OPT ≤ LRU ≤ FIFO usually. Faults ≥ number of distinct pages. LRU and OPT are stack algorithms.
Exam notes (the dense version)

When no frame is free, a policy picks the victim. FIFO: evict the page that has been in memory longest. LRU: evict the page not used for the longest time (good approximation of the future, costly to implement exactly; hardware uses reference bits, the clock / second-chance algorithm). Optimal (OPT): evict the page used farthest in the future: the minimum possible faults, but impossible in practice, so it is the benchmark. To count faults draw one column per reference and mark hits and faults.

Previous year questions: Virtual Memory (9)

PYQs are grouped by topic, so this is the full set for Virtual Memory; try the ones that match this subtopic. Open all 9 on the topic page.