What are page replacement algorithms?

Why Interviewers Ask This

This question targets practical, hands-on experience with Operating Systems. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

When a page fault occurs and there are no free frames, the OS must evict (replace) a page to make room. The page replacement algorithm determines which page to evict. Optimal (OPT/Belady): replace the page that won't be used for the longest time in the future. Theoretically optimal (fewest page faults) but impossible to implement (requires knowing the future). Used as benchmark. FIFO (First In First Out): replace the oldest page in memory. Simple (just a queue). Problem: Belady's Anomaly — adding more frames can INCREASE page faults! Counter-intuitive. Example: with 3 frames, access pattern 1,2,3,4,1,2,5,1,2,3,4,5: 9 faults. With 4 frames: 10 faults! LRU (Least Recently Used): replace the page not used for the longest time. Uses past as predictor of future. No Belady's anomaly. Good approximation of OPT. Implementation: timestamp each page access, choose minimum timestamp for eviction. Expensive: hardware counter or stack per page access. Approximations used in practice. LRU approximations: Clock algorithm (second chance): FIFO but give each page a second chance using a reference bit. If bit=1 when page would be evicted, clear bit and skip to next page. Clock hand sweeps through frames. LFU (Least Frequently Used): replace page with lowest access count. Recent behavior not captured well. Aging can help. NFU (Not Frequently Used) / NRU (Not Recently Used): use reference and dirty bits. Prefer clean over dirty pages (saving dirty requires disk write). Linux uses: variant of clock algorithm with multiple lists (active, inactive).

Common Mistake

Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong Operating Systems candidates.