advanced23 min studyLesson 145 in Engineering

Page replacement and working sets

Use reference traces, reuse distance, and working-set windows to explain why a resident footprint can be too large or too small for a workload.

On this page

What you’ll understand

  • Compute working-set size for an explicit reference window.
  • Simulate a small fully associative LRU cache without mistaking it for Linux reclaim.
  • Connect refaults and access locality to useful capacity decisions.

The amount of memory a process has mapped is not the amount it needs right now. Replacement decisions try to keep pages whose future use justifies their physical cost, although the future is unknown. A reference trace makes that uncertainty concrete: a large cold mapping can be cheap, while a smaller repeatedly reused set can suffer badly if it does not fit.

A working set needs a time or reference window

Define a working set as the distinct pages referenced within a stated recent interval. That definition needs both a page granularity and a window. A short window can describe a hot inner loop; a longer one may include the model weights and input blocks needed across several requests. Neither number is universally correct. A service alternating between two phases can have small per-phase working sets but a much larger union. Record the phase and interval before comparing estimates or promising that a machine has enough memory.

Resident-set size counts pages currently resident under a metric's rules, including pages that may not have been used recently. It can therefore exceed a current working-set estimate. It can also be smaller than the pages the workload will shortly need, causing repeated loading or reconstruction. Sharing complicates host totals further. When planning placement, distinguish an application's useful hot data, reclaimable cold cache, kernel overhead, pinned memory, and room for bursts. A single resident-size snapshot is not a complete future demand forecast.

W(t,Δ)={pi∣t−Δ<ti≤t},MW=P∣W(t,Δ)∣W(t,\Delta)=\{p_i\mid t-\Delta<t_i\le t\},\qquad M_W=P\lvert W(t,\Delta)\rvert
p_i is the page referenced at time t_i. The window Δ uses the same time units as t, P is bytes per page, and M_W is the byte size of distinct referenced pages. This is a trace definition, not a claim that every page remains resident.↔ Scroll wide equations horizontally.

A tiny trace exposes the replacement tradeoff

For a fully associative least-recently-used teaching cache, retain the most recently referenced distinct pages up to capacity. A hit moves its page to the most-recent position. A miss inserts it and evicts the least-recent page when full. Count cold misses separately from misses caused by insufficient capacity when the distinction helps. This small model is useful because you can verify each decision by hand. Real operating-system reclaim has multiple page classes, writeback costs, protection constraints, and sampled access information rather than an exact global reference order.

Reuse distance counts distinct intervening pages between two references to the same page. In the ideal fully associative LRU model, a repeated reference hits when that distance is below capacity in pages. Consider a sequential scan repeatedly cycling through one more page than the cache holds: the newest page displaces a page needed again soon. Repeated refaults can consume capacity without useful progress, a form of thrashing. Blocking work into smaller reusable regions may help, but an algorithm with no reuse might instead benefit from avoiding pollution of valuable hot data.

LRU hit on reuse  ⟺  d<C\text{LRU hit on reuse}\iff d<C
d is the number of distinct other pages referenced since the previous reference, and C is fully associative LRU capacity in pages. This exact condition assumes an initially managed cache with no external invalidation, pinning, prefetching, or bypass and does not describe all Linux reclaim policies.↔ Scroll wide equations horizontally.

Use refault evidence to test a capacity hypothesis

Linux reclaim policies evolve and may use mechanisms such as multi-generation aging rather than a literal textbook LRU stack. Read the deployed kernel's counters and interfaces before interpreting a named statistic. A rise in refault activity alongside memory stalls and lower useful throughput supports a locality-pressure hypothesis; a fault count by itself does not establish it. Compare controlled changes such as a smaller batch or fewer workers while preserving the same useful output. That helps distinguish insufficient useful capacity from a slow storage device or unrelated CPU contention.

The probability lab supplies a contrast to a correlated reference trace. Set success probability to 0.75 and twenty trials, predicting fifteen hits in an independent constant-probability model. Keep those controls fixed and imagine two traces: five misses evenly distributed versus five misses clustered in a burst. The binomial distribution does not encode either order, so it cannot reproduce their latency or LRU outcome. Use that limitation to explain why average hit probability alone is an inadequate working-set measurement, even though its expected count is simple.

A loop just larger than available capacity

Start an empty fully associative LRU cache with capacity three pages. Access the trace A, B, C, A, D, B. Count misses and compare with capacity four.

  1. A, B, and C are three cold misses. The next A hits, leaving recency order B, C, A from least to most recent.

  2. D misses and evicts B. The final B therefore misses and evicts C. At capacity three, the six references produce five misses and one hit.

  3. With capacity four, A, B, C, and D are the only misses. Both repeated A and final B hit, giving four misses. The full trace contains four distinct pages, while a recent-window working set can contain fewer.

The result

One extra frame removes the final refault in this trace. The calculation is exact for the stated LRU model and is not a prediction of a kernel's replacement sequence.

Explore the model

Interactive lab

Interactive lab · probability

An average hit rate loses reference order

With p=0.75 and n=20, predict fifteen hits and five misses on average in the independent binomial model. Compare that aggregate with an imagined clustered trace. The bars contain no replacement state, reuse order, or Linux working-set estimator.

An average hit rate loses reference orderThese are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forExpected counts are useful, but working-set pressure depends on when distinct pages are reused.

Check your understanding

In an ideal fully associative LRU cache of four pages, a page is reused after three distinct other pages. With no invalidation, what happens?

Try it yourself

For trace A, B, A, C, D, A, count distinct pages in the last four references and determine the reuse distance of the final A.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

The last four references are A, C, D, A. Count unique names; then count distinct pages strictly between the two relevant A references.

Compare with the solution

The last-four-reference working set is {A,C,D}, containing three pages. The final A has reuse distance two because only C and D occur since the preceding A. In an ideal fully associative LRU cache it therefore hits with capacity at least three, absent outside interference.

Take it with you

  • Every working-set estimate requires a defined window and page granularity.
  • Reference order and reuse distance explain behavior that average hit rates hide.
  • Treat textbook replacement as a reasoning model and validate real reclaim with measurements.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.