advanced23 min studyLesson 148 in Engineering

Memory overcommit and exhaustion

Separate virtual reservations, commit accounting, physical availability, and cgroup limits when planning allocation failure and recovery.

On this page

What you’ll understand

  • Explain Linux overcommit modes without treating an allocation as a residency guarantee.
  • Calculate a simplified commit limit and remaining accounting headroom.
  • Distinguish host exhaustion, cgroup enforcement, and application-level load shedding.

A successful allocation request can establish an address range or accounting commitment before all of its pages are resident. Overcommit policy governs some of those promises, while physical allocation and administrative limits impose additional constraints later. Reliable services need to distinguish these layers, check immediate errors, and avoid discovering an unbounded workload's true memory requirement only after it has accepted irreplaceable work.

Reservation, commitment, and residency answer different questions

A virtual reservation describes an address-space range. Commit accounting tracks certain potential private writable memory demands according to operating-system rules. Residency measures physical backing currently present. These values need not match, particularly for sparse mappings, shared file pages, and copy-on-write. Linux exposes CommitLimit and Committed_AS for system commit accounting, but their difference is not the number of free physical bytes. Counting only resident usage can miss promises that become physical demand together during a burst or a child's later writes.

Linux offers heuristic, permissive, and stricter overcommit policies. Their detailed accounting depends on mapping type; a writable private file mapping is different from a shared read-only file mapping because private modifications may need new anonymous backing. In stricter mode, the configured ratio or byte amount and swap contribute to the accounting limit. This mechanism is useful admission control, but it does not abolish cgroup limits, constrained allocations, stack growth, kernel overhead, hardware failures, or application bugs. Avoid turning a policy description into an unconditional guarantee that no process can ever be killed.

Climit≈S+r100(R−H)C_{\rm limit}\approx S+\frac{r}{100}(R-H)
This ratio-based teaching form uses swap S, physical RAM R, memory H reserved for HugeTLB, all in bytes, and overcommit_ratio r in percent. It applies to Linux strict commit accounting under the ratio setting; explicit overcommit_kbytes and implementation rounding require their documented rules.↔ Scroll wide equations horizontally.

Exhaustion can happen inside a smaller administrative boundary

A container or service cgroup can reach its memory maximum while the host still has spare RAM. Reclaim and enforcement then operate within that resource boundary. In cgroup v2, memory.high is a pressure and throttling boundary, while memory.max is a harder limit with documented exceptions and enforcement behavior. Observe memory.events and the relevant ancestor limits rather than assuming an exit means the host ran out globally. Shared page charging and hierarchy also make it unsafe to derive a cgroup's current charge by simply adding a few process RSS readings.

An OOM decision selects victims under the applicable policy; it does not promise to kill the leaking process or the least valuable user request. Selection controls and protected tasks affect behavior, and userspace memory managers may also terminate workloads before the kernel's final exhaustion path. An abrupt kill may prevent the victim from running cleanup. Design checkpoints, idempotent retries, and durable ownership transitions so that losing one process does not corrupt the job record. A restart loop that reloads the same oversized state can recreate pressure faster than the system recovers.

Bpayload≤L−Mbaseline−Moverlap−MreserveB_{\rm payload}\le L-M_{\rm baseline}-M_{\rm overlap}-M_{\rm reserve}
L is a chosen operational byte budget below applicable limits. Baseline, overlapping temporary memory, and a deliberate reserve are byte estimates. The inequality is planning arithmetic, not an exact Linux charge formula; sharing, kernel charges, and workload variability need measurement.↔ Scroll wide equations horizontally.

Admit bounded work before allocating its peak state

Predict peak overlap rather than only final model size. An update may retain old and new weights simultaneously; a training step may create optimizer state and temporary workspaces after loading succeeds. Cap in-flight jobs by a measured per-job envelope, reject or defer excess work, and test allocation failure paths. Leaving headroom is meaningful only when tied to known uncertainties. A documented smaller batch that completes reliably is often more useful than accepting an arbitrarily large batch and hoping the OOM policy chooses a convenient victim.

The queue lab illustrates an upstream control through two separate runs. Press Reset, explicitly set both arrival four and service three, predict growth of one job per second from the initial four, then Play or Step. Reset again, set both arrival two and service three, predict drainage from four after four seconds, then Play or Step. Editing rates alone retains elapsed time; it does not preserve an existing backlog through a live intervention. If queued jobs retain buffers, bounded arrivals can reduce one source of retained memory. The model calculates no bytes, commit accounting, cgroup charges, or victim selection. Identify the real per-job measurements and rejection behavior needed for a service policy.

A commit budget is not a resident-memory reading

Assume strict ratio-based commit accounting, 64 GiB physical RAM, 8 GiB set aside for HugeTLB, 16 GiB swap, and a 50 percent overcommit ratio. Current committed accounting is 38 GiB.

  1. Subtract the stipulated HugeTLB reservation from RAM for this simplified ratio calculation: 64 − 8 = 56 GiB.

  2. Half of 56 GiB is 28 GiB. Add the 16 GiB swap contribution to obtain an illustrative CommitLimit of 44 GiB.

  3. Subtract current committed accounting: 44 − 38 = 6 GiB of accounting headroom. This is neither a report of free RAM nor proof that any arbitrary 6 GiB allocation will pass cgroup, alignment, address-space, and other constraints.

The result

A commit limit controls one kind of promise. Budget physical peaks and administrative limits separately, and inspect the actual host's reported limit.

Explore the model

Interactive lab

Interactive lab · queue

Bound retained work before pressure escalates

Reset, explicitly set both λ=4 and μ=3, predict backlog 4+t, then Play or Step. In a separate run, Reset, set both λ=2 and μ=3, predict drainage from four at t=4, then Play or Step. Rate edits alone retain elapsed time. Normalized jobs have no byte charges or OOM policy; the M/M/1 mean has separate assumptions.

Bound retained work before pressure escalatesFluid 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 forAdmission control can bound one source of memory demand, but a real memory budget also needs per-job and shared-state measurements.

Check your understanding

A cgroup reaches memory.max and suffers an OOM event while the host still has available RAM. Which interpretation is sound?

Try it yourself

A service chooses a 20 GiB operating budget. Baseline state uses 8 GiB, update overlap needs 4 GiB, and reserve is 2 GiB. Each concurrent job needs an estimated 1.5 GiB at peak. What concurrency fits this planning model?

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

Give me a hint

Subtract non-job memory before dividing by peak job bytes.

Compare with the solution

The payload allowance is 20 − 8 − 4 − 2 = 6 GiB, so at most four simultaneous 1.5 GiB jobs fit the arithmetic envelope. This is a planning cap, not a kernel guarantee. Validate actual charges, shared buffers, allocation variability, and overlap before enforcing it as a production admission policy.

Take it with you

  • Reservation, commitment, and residency are separate memory quantities.
  • Cgroup limits can bind even when the host has available RAM.
  • Plan peak overlap, bounded admission, and recovery before relying on exhaustion behavior.

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.