advanced23 min studyLesson 146 in Engineering

Swap and memory reclamation

Follow clean discard, dirty writeback, anonymous swapping, and direct reclaim while measuring whether reclamation actually helps useful work.

On this page

What you’ll understand

  • Explain why different page classes have different reclamation costs.
  • Distinguish swap occupancy from active swap traffic and memory stalls.
  • Estimate a reclamation bandwidth bound without treating swap as equivalent RAM.

Reclaim makes physical pages available for new uses, but not every occupied page can be freed in the same way. Some cached content can be discarded, modified data must be preserved, and some allocations cannot currently move at all. Swap adds a place to preserve eligible anonymous content, with its own costs and limits. The goal is useful progress, not the smallest possible cache or swap counter.

Find another authoritative copy before discarding

A clean file-backed cache page can often be dropped because the file supplies its contents again. A dirty file-backed page ordinarily needs appropriate writeback before that physical storage can be reused without losing changes. Anonymous private state has no original file copy to reread, so reclaim may preserve it in swap if eligible and swap is available. The word anonymous does not mean unimportant; it can contain a live request, a model's mutable state, or a language runtime heap. Losing that content is not an acceptable eviction policy.

Some physical memory is unreclaimable at the moment, for example because a driver holds a relevant pin or kernel state is still actively required. Other kernel caches may be shrinkable through their own mechanisms. Reclaimability is therefore not a single property of all nonfree RAM. Before changing policies, ask which pages dominate and why they remain needed. A system can show considerable memory in caches and still be healthy because those caches are useful and can shrink; forcing them empty may only create future misses and storage traffic.

Mreclaimed=P(Nclean+Nwritten+Nswapped)M_{\rm reclaimed}=P(N_{\rm clean}+N_{\rm written}+N_{\rm swapped})
The counts describe disjoint pages actually made reusable during the measured interval, and P is bytes per page in this simplified base-page accounting. Scanned pages that remain pinned, hot, dirty, or otherwise retained do not count as reclaimed.↔ Scroll wide equations horizontally.

Background and direct reclaim affect latency differently

Background reclaim tries to restore free-page reserves before an allocation is forced to do substantial work itself. Under stronger pressure, an allocating task can enter direct reclaim and wait while pages are examined, written, or otherwise made available. Allocation latency can then reflect memory-management work unrelated to the tiny object the application requested. Reclaim behavior depends on the zone, node, cgroup, allocation constraints, and kernel policy. Global free bytes alone cannot prove that a particular constrained allocation is easy to satisfy.

Swap occupancy says that some eligible content has a swap representation or is accounted as swapped under the metric being read. It does not, on its own, mean that the machine is currently transferring that many bytes per second. A cold inactive page remaining swapped out can be harmless. Repeatedly swapping useful pages out and faulting them back in can destroy latency and throughput. Correlate swap-in and swap-out rates, reclaim efficiency, pressure-stall information, and useful completions. Changing a swappiness preference does not create extra device bandwidth or make anonymous and file-backed pages equally costly.

Tout≥DoutBout,Tin≥DinBinT_{\rm out}\ge\frac{D_{\rm out}}{B_{\rm out}},\qquad T_{\rm in}\ge\frac{D_{\rm in}}{B_{\rm in}}
D_out and D_in are actual bytes transferred and B_out and B_in are sustained directional bandwidths in bytes per second. Each is a lower bound for its transfer phase; compression, caching, overlap, random-I/O latency, and shared-device contention need separate treatment.↔ Scroll wide equations horizontally.

Reclaim should buy more progress than it costs

A useful experiment reduces the demand that creates churn: shrink a batch, cap concurrent workers, release no-longer-needed buffers, or move a cold phase elsewhere. Compare useful throughput and latency before and after, along with memory and storage counters. Adding swap may prevent abrupt failure yet still leave a workload too slow to serve its users. Conversely, disabling swap indiscriminately can force earlier exhaustion or evict valuable file cache. Choose a policy using a workload-specific service objective and tested recovery behavior rather than treating zero swap use as an absolute virtue.

In the queue lab, interpret arrivals as normalized requests for reclaim work and service as reclaim completion capacity. Press Reset, explicitly set both arrival two and service four, predict drainage of the initial four-job backlog in two seconds, then Play or Step. For a separate comparison, Reset, set both arrival two and service one, predict one-job-per-second growth from four, then Play or Step. Rate edits alone retain elapsed time and recompute the initial-value model. This fluid model has no clean/dirty classifier, device latency, or OOM policy. Explain why faster scanning need not free pages faster when many scanned pages remain ineligible.

How much scanned memory became useful capacity?

In an illustrative base-page interval, reclaim scans 10,000 pages. It frees 2,000 clean file pages, finishes writeback and frees 500 dirty file pages, and swaps out and frees 1,000 anonymous pages. Pages are 4 KiB.

  1. Count only pages actually made reusable: 2,000 + 500 + 1,000 = 3,500. The remaining 6,500 scanned pages do not contribute to the freed total.

  2. The reclaimed capacity is 3,500 × 4,096 = 14,336,000 bytes, about 13.67 MiB. The scan-to-reclaim yield is 3,500 / 10,000 = 35 percent for this simplified interval.

  3. The 1,000 swapped pages contain 4,096,000 bytes of uncompressed payload. At an assumed sustained 100 MiB/s, transferring that payload alone requires at least 0.0390625 seconds, before metadata, contention, and future reads.

The result

Scan activity is work performed; reusable pages are the capacity obtained. Both quantities matter when diagnosing expensive reclaim.

Explore the model

Interactive lab

Interactive lab · queue

Reclaim demand can outrun completion

Reset, explicitly set both λ=2 and μ=4, predict the initial four-job backlog drains at t=2, then Play or Step. Separately Reset, set both λ=2 and μ=1, predict growth from four, then Play or Step. Rate edits alone retain elapsed time. Equal jobs illustrate imbalance; this is not a reclaim, swap-device, or OOM simulator.

Reclaim demand can outrun completionFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. 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 forMore scanning or swapping activity is not automatically evidence that useful memory demand is under control.

Check your understanding

A machine has 4 GiB of swap occupied but almost no current swap traffic or memory stalls. What can that observation support?

Try it yourself

A service must fault back in 256 MiB of uncompressed anonymous data from a device sustaining 128 MiB/s. What transfer lower bound follows, and why is it not a response-time prediction?

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

Give me a hint

Divide payload by sustained bandwidth and identify omitted work.

Compare with the solution

The data-transfer lower bound is two seconds. Actual response time can be longer because of random I/O, scheduling, page-table work, allocation and reclaim, shared-device contention, and application computation. Some work might overlap, but that does not justify substituting the bandwidth bound for a measured latency distribution.

Take it with you

  • Reclamation depends on page type, backing state, and current constraints.
  • Swap occupancy, swap traffic, and memory-stall time are distinct observations.
  • Measure reclaimed capacity and useful throughput rather than celebrating scan activity alone.

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.