Page replacement algorithm

On this page 7

A page replacement algorithm is the policy an operating system uses to choose which resident page to evict from physical memory when a page fault occurs and no free page frame is available. In memory management, it is the component that turns a fixed amount of RAM into the illusion of a much larger virtual address space, and its quality largely decides whether a loaded system stays responsive or slides into thrashing.

Modern operating systems such as Linux, Windows and macOS do not run a single textbook algorithm. They combine approximations of least recently used behavior, background reclaim threads and separate treatment of file-backed and anonymous memory, but every production design still answers the same question: which page is least likely to be needed soon.

How page replacement works during a page fault

Page replacement runs when the memory management unit reports a reference to a page that is not resident and the kernel finds no free frame. The kernel selects a victim page, writes it to backing storage if it is dirty, updates the page table entry to mark it non-resident, invalidates any cached translation in the translation lookaside buffer, and finally loads the requested page into the reclaimed frame.

The cost of a wrong choice is asymmetric. A memory access takes on the order of 100 nanoseconds, while servicing a major fault from an SSD takes tens to hundreds of microseconds and from a hard disk several milliseconds. Even a fault rate of one in a million accesses can therefore double effective memory latency, which is why replacement quality matters far more than the cost of the algorithm itself.

The following table lists the hardware and kernel signals that replacement policies rely on and what each one tells the kernel.

SignalSourceWhat it indicates
Referenced (accessed) bitSet by the MMU on any accessPage was used since the bit was last cleared
Dirty (modified) bitSet by the MMU on a writePage must be written back before eviction
Page typeKernel bookkeepingFile-backed pages can be dropped, anonymous pages need swap
Pinned or locked flagKernel or mlock callPage must not be evicted at all
Fault historyKernel shadow entriesPage was evicted recently and is being refaulted
Signals used to choose a victim page

Classic page replacement algorithms

The textbook algorithms differ mainly in how much history they keep and how expensive that history is to maintain. The theoretical benchmark is the optimal algorithm (OPT or MIN), which evicts the page whose next use lies furthest in the future; it cannot be implemented because it requires knowledge of future references, but it gives a lower bound for comparing real policies on recorded traces.

The table below compares the algorithms most often discussed in operating systems courses and literature, together with their practical standing.

AlgorithmEviction ruleOverheadPractical standing
Optimal (OPT)Page used furthest in the futureNeeds future knowledgeBenchmark only
FIFOOldest loaded pageVery lowPoor alone, suffers Belady’s anomaly
LRULeast recently used pageHigh if exactGood model, approximated in practice
Clock (second chance)First page with referenced bit clearLowWidely used basis
NRULowest class by referenced and dirty bitsLowSimple, coarse
LFULeast frequently used pageModerateKeeps stale popular pages too long
WSClockClock plus working set ageModerateGood for multiprogramming
Comparison of classic algorithms

Exact LRU requires updating a timestamp or reordering a list on every memory access, which hardware does not support for main memory. That is why the Clock algorithm dominates real kernels: it sweeps a circular list, clears referenced bits as it passes, and evicts the first page found with the bit still clear, giving LRU-like behavior at a fraction of the cost.

Replacement in modern operating systems

Production kernels refine the classic ideas to resist scan pollution, where a single large sequential read pushes the entire hot working set out of memory. The common remedy is to keep separate active and inactive lists so that a page must prove itself with a second access before it is treated as hot.

  • Linux historically used active and inactive LRU lists per memory cgroup, split into file-backed and anonymous pages, and since version 6.1 offers the multi-generational LRU (MGLRU), which ages pages across several generations and scans page tables more efficiently.
  • Windows manages a working set per process, trims pages to standby and modified lists, and reuses standby pages without disk access if they are referenced again before being repurposed.
  • Database buffer pools and storage caches often use ARC, 2Q or LRU-K, which weigh recency and frequency together, although these designs are rarer in general-purpose kernels.

Kernels also distinguish global replacement, where any process can lose frames to another, from local replacement, where a process replaces only its own pages. Global policies use memory more efficiently, while local policies isolate workloads; container memory limits are a modern form of local reclaim boundary.

Choosing and tuning replacement behavior

Administrators rarely select an algorithm directly, but they tune the inputs that the kernel policy uses. The decision table below maps common situations to adjustments and the reasoning behind them.

SituationRecommended adjustmentReason
Interactive desktop under pressureEnable MGLRU or keep defaultsBetter protection of active pages
Database with its own cacheLower swappiness, use direct I/OAvoids double caching and swap storms
Latency-critical serviceLock hot memory with mlockPrevents eviction of critical pages
Many containers on one hostSet memory limits and reservationsConfines reclaim to each workload
Large sequential backupsUse read-ahead hints or fadviseStops scans from flushing the cache
Tuning decisions for page replacement

On Linux, the swappiness parameter biases reclaim between dropping file pages and swapping anonymous pages. Its default of 60 suits general use; values near 10 favor keeping application memory resident, while higher values suit systems with fast swap such as zram.

Limitations and common misconceptions

No replacement algorithm can compensate for a working set larger than physical memory. When the sum of active working sets exceeds RAM, every policy degrades into constant faulting, and the only real fixes are adding memory, reducing load or killing processes through an out of memory handler.

SymptomLikely causeFix
High major fault rate, idle CPUWorking set exceeds RAMAdd memory or reduce concurrency
Cache emptied after a backup jobScan pollutionUse fadvise or MGLRU
More frames, more faultsBelady’s anomaly under FIFOUse a stack algorithm such as LRU
Swap use with plenty of free RAMReclaim of cold anonymous pagesUsually normal, adjust swappiness
Symptoms, causes and fixes

A frequent misconception is that swap use always signals a problem. Moving genuinely cold anonymous pages to swap frees frames for the page cache and often improves overall performance. Another misconception is that LRU is always best: for cyclic access patterns slightly larger than memory, LRU evicts exactly the page needed next, and random or frequency-aware policies perform better.

Explained without jargon

Think of a small desk and a large bookshelf across the room. You can only keep a few books open on the desk, so when you need a new one and the desk is full, you must carry one back to the shelf. A page replacement algorithm is the rule for deciding which book goes back.

A good rule returns the book you are least likely to open again soon, often the one you have not touched for the longest time. A bad rule keeps sending back the book you need next, and you spend all day walking instead of reading. Computers face the same choice with memory, and a smart choice keeps the machine fast.

A typical use case

A hosting team runs a Linux server with 64 GB of RAM that serves a web application and also runs a nightly backup reading about 500 GB of files. Every morning, page load times rise from about 80 milliseconds to over 400 milliseconds for the first hour, because the sequential backup read has pushed the application’s hot pages and cached files out of memory.

The team enables the multi-generational LRU, adds fadvise hints with the POSIX_FADV_DONTNEED flag to the backup tool so it drops pages after reading them, and lowers swappiness from 60 to 20. After the change, morning response times stay near 90 milliseconds, the major fault rate during the first hour falls by roughly an order of magnitude, and no extra memory had to be purchased.