Infrastructure
Linux: diagnose an OOM kill before adding memory
A production runbook for separating a cgroup limit from host-wide memory pressure, containing a Linux service and validating a bounded fix.
An API on an Azure Linux VM restarts during a processing spike. The logs say a process was killed for lack of memory, yet the host dashboard still shows available memory. Resizing the VM immediately may not help: the service could be constrained by its own control group or a parent group.
The running example is a systemd service, orders-api.service, on a cgroup v2 host. Commands are read-only unless stated otherwise. Adapt the service name, permissions and paths to your distribution. Cgroup v1 and containers expose different files or visibility; verify the environment before using these paths.
Contain the impact and preserve evidence
Remove the instance from traffic if it is failing or could leave partial writes. Check the remaining instances have capacity and limit incoming load if needed. Do not repeatedly rerun a non-idempotent job: establish which operations were accepted and their outcomes before replay.
Record UTC time, instance, deployed version, load, recent changes and restart time. Preserve logs before rotation; counters can disappear when a cgroup is removed or recreated. Logs may contain sensitive data.
date -u --iso-8601=seconds
findmnt -t cgroup2
systemctl status orders-api.service --no-pager -l
systemctl show orders-api.service -p ControlGroup -p MainPID -p Result -p MemoryCurrent -p MemoryMax
journalctl -u orders-api.service --since "2 hours ago" --no-pager
journalctl -k --since "2 hours ago" --no-pager | grep -Ei 'out of memory|oom|killed process'
free -h
cat /proc/pressure/memory Missing kernel messages do not rule out a memory-related termination: logs may be unavailable, and a userspace manager can also terminate a process. Correlate PID, cgroup, time and systemd result; an exit code alone does not establish the cause.
Separate a local limit from host pressure
Get the active service’s ControlGroup with systemctl show. On a host with cgroup v2 mounted at /sys/fs/cgroup, inspect that group and each parent up to the root. Replace the example path with the observed value and check permissions and file availability.
CG=/sys/fs/cgroup/system.slice/orders-api.service
cat "$CG/memory.current"
cat "$CG/memory.max"
cat "$CG/memory.high"
cat "$CG/memory.events"
cat "$CG/memory.events.local"
cat "$CG/memory.stat"
systemctl show orders-api.service -p MemoryHigh -p MemoryMax -p Slice memory.current is a snapshot, not the peak before termination. A value of max means no explicit limit at that level, not that parents are unlimited. By default memory.events counters are cumulative and hierarchical; memory.events.local isolates the level being read. Take two timestamped samples and compare deltas, allowing for cgroup recreation.
An increase in max and oom at a limit, correlated with logs, points towards that limit. oom_kill counts processes in the group killed by any OOM killer: it does not by itself prove a local cgroup OOM. Conversely, sustained low available host memory, high system pressure and events across services point towards host pressure. Memory pressure measures time stalled, not free bytes. Check swap, neighboring processes and historical metrics too: a post-restart snapshot may look normal.
Find the consumer before changing the limit
Use memory.stat to distinguish anonymous memory from file cache, among other categories; do not blindly add categories and their subcategories. Correlate the trend with concurrency, batch size, deployments and traffic. Process RSS does not exactly match cgroup accounting, which includes other charges. A large cache alone is not evidence of a leak.
If growth follows a deployment, compare with a healthy instance at a known version and load. If it follows an unusual batch, retain its size and identifier without exposing its contents. Check effective parent slice limits and host headroom as well. An undersized limit, an application leak and host saturation require different responses.
Choose a bounded correction
- Confirmed limit and legitimate workload: size headroom from observed peaks, host capacity and neighboring services. Trial a higher limit on one instance with an owner, duration and ceiling. Do not remove the limit by default.
- Application regression or abnormal batch: reduce concurrency or return to a known version using the deployment procedure. Check completed writes and data compatibility before rollback.
- Host-wide pressure: reduce load or add capacity after checking consumers and placement constraints. A larger VM does not automatically change a cgroup limit.
Before changing systemd configuration, capture both configured and effective values, obtain the required approval and check whether applying the change requires a restart. A parent change may affect several services. If the cause is still uncertain, keep the instance out of traffic and continue collecting evidence.
Validate, then roll back if needed
On the canary, run a representative, bounded workload without duplicating business effects. Compare errors, latency, availability, memory consumption and pressure, plus changes in local and parent counters over a window that includes the peak. Check business outcomes and the health of other services on the same host.
Stop the trial if host pressure rises dangerously, neighboring services degrade or errors persist. Remove the canary from traffic, restore the prior configuration or version using the approved procedure, then recheck the effective value and service. Restoring the original limit can reproduce the OOM: do not return the instance to load until capacity and cause are controlled.
Conclusion
The decision follows the evidence: service limit, parent limit or host pressure. Expand a fix only when a canary handles representative load, neighboring services remain healthy and rollback is viable.