DSA, Database System & Operating System β Memory Management, File Systems & Administration, NEC licence examination syllabus (Nepal Engineering Council).
Page Replacement Algorithms
Choosing which page to evict β FIFO, Optimal, LRU and Clock, computed on one reference string.
π Where this lives: the same algorithms run in every cache you use. Your CPU's L3 cache uses an LRU approximation; Redis offers LRU and LFU eviction by configuration; a CDN decides which objects to keep; your browser evicts cached images. The reason approximations dominate exact LRU is measured below β true LRU needs bookkeeping on every access, which is unaffordable in hardware. Every production cache is therefore some variant of the Clock algorithm. Search "Redis maxmemory-policy allkeys-lru" to see the same menu of choices exposed as configuration.
The problem
When a page fault occurs and NO free frame exists, one resident
page must be evicted. The choice determines the fault rate,
which (from the previous topic) dominates performance.
If the victim is DIRTY it must be written to disk first β
doubling the cost. Hence the DIRTY BIT: prefer a clean
victim, because discarding it is free.
FRAME ALLOCATION POLICIES:
EQUAL each of n processes gets frames/n
PROPORTIONAL each gets frames Γ (its size / total size)
PRIORITY higher-priority processes get more
LOCAL replacement a process may only evict its OWN pages
β its fault rate depends only on itself
GLOBAL replacement a process may evict ANY process's page
β better overall utilisation, but a
process's performance now depends on
others' behaviour, so it is
unpredictable
Most systems use global replacement, accepting the
unpredictability for the better throughput.
The algorithms, computed on one reference string
REFERENCE STRING (the standard textbook example, 20 refs):
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
All results below were COMPUTED, not estimated.
ββββββββ WITH 3 FRAMES ββββββββ
algorithm faults hit rate
FIFO 15 25.0%
OPTIMAL 9 55.0%
LRU 12 40.0%
CLOCK 14 30.0%
ββββββββ WITH 4 FRAMES ββββββββ
algorithm faults hit rate
FIFO 10 50.0%
OPTIMAL 8 60.0%
LRU 8 60.0%
CLOCK 9 55.0%
1. FIFO β evict the OLDEST page
Β· a simple queue; the page that arrived first leaves first
β trivial to implement β one pointer
β WORST performance here (15 faults with 3 frames)
β ignores usage entirely: a heavily used page is evicted
just because it is old
β suffers BELADY'S ANOMALY (below)
2. OPTIMAL (OPT / MIN) β evict the page that will not be used
for the LONGEST TIME in the future
β provably the BEST possible: 9 faults, the theoretical
minimum
β IMPOSSIBLE to implement β it requires knowing the future
β its value is as a BENCHMARK. FIFO's 15 versus OPT's 9
tells you exactly how much room for improvement exists.
3. LRU β evict the LEAST RECENTLY USED page
Β· uses the past as a prediction of the future, which works
because of temporal locality
β good performance: 12 faults, much closer to OPT than FIFO
β NO Belady's anomaly (it is a "stack algorithm")
β EXPENSIVE to implement exactly:
counter method β timestamp every page on every access
stack method β move the page to the top of a stack on
every access
Both require work on EVERY MEMORY REFERENCE, which
hardware will not do.
4. CLOCK (second-chance) β the practical LRU approximation
Β· pages in a circular list, each with a REFERENCE BIT
Β· to evict: examine the page at the hand
ref = 0 β EVICT it
ref = 1 β set ref = 0, advance, give it a second chance
β needs only ONE bit per page, set by hardware for free
β 14 faults with 3 frames β between FIFO (15) and LRU (12)
β this is what real operating systems use
ENHANCED CLOCK uses the reference AND dirty bits, giving four
classes and preferring (0,0) β not recently used and clean,
so no write-back needed.
Belady's anomaly β verified
Intuition says more frames must mean fewer faults. For FIFO
that is FALSE.
REFERENCE STRING: 1 2 3 4 1 2 5 1 2 3 4 5
MEASURED:
FIFO with 3 frames: 9 faults
FIFO with 4 frames: 10 faults β WORSE with MORE memory
LRU with 3 frames: 10 faults
LRU with 4 frames: 8 faults β improves, as expected
Adding a frame made FIFO worse. That is BELADY'S ANOMALY, and
it is not a rounding artefact β it is a structural property of
FIFO.
WHY IT HAPPENS: FIFO's eviction choice ignores usage. With more
frames, a page can survive long enough to be at the FRONT of
the queue exactly when it is about to be needed again β so it
is evicted at the worst possible moment. More memory changed
the queue order for the worse.
STACK ALGORITHMS are immune. An algorithm is a stack algorithm
if the set of pages in n frames is always a SUBSET of the set
in n+1 frames. LRU and OPT satisfy this; FIFO does not.
For a stack algorithm, adding a frame can only ever help,
because everything that was resident before is still
resident.
WHY THIS MATTERS BEYOND THE EXAM: it means you cannot tune a
FIFO cache by simply making it bigger and measuring. The
relationship is not monotonic, so a benchmark at one size does
not predict another. LRU-family caches are safe to size by
measurement; FIFO ones are not.
The gap between FIFO (15) and OPTIMAL (9) at three frames is the practical message: a better eviction policy cut faults by 40% with no extra memory. Since a fault costs ~40,000 memory accesses on a spinning disk, that 40% is enormous β which is why every real system pays for Clock's reference bit rather than using plain FIFO.
Other algorithms, and the trade they make
LFU (least frequently used) β evict the lowest access count
β a page used heavily once then never again keeps a high
count forever
β a newly loaded page has a low count and is evicted
immediately, even if it is about to be used
FIX: AGING β periodically halve all counters, so old activity
decays.
MFU (most frequently used) β evict the highest count, arguing
that a low-count page has just arrived and will be used
β performs poorly in practice; mainly of theoretical interest
SECOND-CHANCE / CLOCK β described above, what real systems use
ENHANCED SECOND CHANCE β reference and dirty bits together:
(0,0) not referenced, not modified β BEST victim
(0,1) not referenced, modified β must write out
(1,0) referenced, not modified β likely reused
(1,1) referenced and modified β WORST victim
Scan for class (0,0) first; if none, scan again clearing
reference bits.
WORKING SET / WSCLOCK β evict pages outside the working set,
combining replacement with the thrashing control from the
swapping topic.
THE PRACTICAL SUMMARY:
OPTIMAL the benchmark β unimplementable
LRU the goal β too expensive exactly
CLOCK the compromise that ships
FIFO only when simplicity outweighs everything
And the reason approximation is acceptable: the difference
between CLOCK (14) and LRU (12) is much smaller than the
difference between FIFO (15) and LRU (12), so one cheap bit
recovers most of the benefit. That is a very common shape in
systems design β the first approximation captures most of the
value.
π Go further: LRU has a well-known weakness that production caches must handle: a single large sequential scan evicts the entire working set, because every scanned page looks "recently used". The fixes are worth knowing β ARC (Adaptive Replacement Cache) keeps separate recency and frequency lists and self-tunes between them; 2Q and LIRS take similar approaches; PostgreSQL uses a clock-sweep variant that deliberately limits how much a sequential scan can pollute the buffer pool. Search "ARC cache algorithm adaptive replacement" and "scan resistant cache".
π‘ Exam angle: the guaranteed question is a trace β given a reference string and a frame count, show the frame contents at each step and count faults for FIFO, OPTIMAL and LRU. Draw the frames as a table; marks come from the working, not just the total. Know that OPTIMAL is unimplementable and used as a benchmark, that LRU is expensive exactly and Clock approximates it with one reference bit, and be able to state and demonstrate Belady's anomaly for FIFO along with why LRU is immune (stack algorithm).
Syllabus points
FIFO, LRU, Optimal (numerical)
Belady's anomaly
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Memory Management, File Systems & Administration