Diagnosing a Kernel OOM Kill in a Containerized JSON Service
System DesignModel answerLast updated
Walk through confirming a kernel OOM kill, cutting memory use of a cache-heavy service without adding RAM, and reading RSS versus VSZ during an investigation.
Service Killed by the Kernel
You are on call for a high-throughput service running in a Linux container. It parses large JSON request bodies and keeps a custom in-process cache to speed up lookups. An alert tells you the kernel's Out-Of-Memory (OOM) killer terminated the service.
What to cover
- Confirm and attribute. Which logs, files, and command-line tools would you use to verify that an OOM kill happened and to identify which process was chosen? How would you tell whether the container hit its own limit or the whole host ran out of memory?
- Mitigate without more RAM. Suppose the cache is the main memory consumer. What changes, at both the application level and the OS/cgroup level, would reduce the chance of another OOM without simply raising the container's memory limit?
- RSS vs VSZ. Explain the difference between Resident Set Size and Virtual Memory Size, and how each helps or misleads you when investigating the memory spike.
Constraints
- Assume Prometheus, Datadog, and similar tools are unavailable. Start from host-level investigation.
- Focus on Linux memory mechanics.
- Consider both application configuration and OS-level limits such as cgroups and
ulimit.
Structure your answer as a clear process, not a list of unrelated commands.
Hints
Hint 1
The kernel is the authority on why a process died. Look at its ring buffer and at the constraint field in the kill message, which tells you whether a cgroup limit or global memory exhaustion triggered it.
Hint 2
Containers have their own accounting. Exit code 137 and the cgroup's memory.events, memory.peak, and memory.stat files show past kills and what kind of memory (anonymous, file, kernel) was in use.
Hint 3
A cache bounded by entry count is not bounded in memory when values vary in size. Think about byte budgets, eviction, and how JSON parsing multiplies payload size in memory.
Hint 4
VSZ counts address space that is mapped but may never be touched. Only resident pages consume physical RAM and count toward the OOM score.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Work in order: confirm, scope, attribute, mitigate, prevent. A strong answer separates a container-level OOM from a host-level OOM and avoids treating "give it more memory" as a fix.
1. Confirm the OOM and find the victim
The kernel is the source of truth.
dmesg -T | grep -iE 'out of memory|oom-kill|killed process'
journalctl -k --since "1 hour ago" | grep -i oom
grep -i oom /var/log/kern.log /var/log/syslog /var/log/messages # depends on distro
In the kill record, check:
constraint=CONSTRAINT_MEMCG: the container exceeded its own cgroup limit.CONSTRAINT_NONE: the host as a whole ran out, so look at neighbors, overcommit, and node reservations.anon-rssvsfile-rss: anonymous memory (heap) is the usual culprit; file-backed pages can normally be reclaimed.- The per-task table printed above the kill line shows each process's RSS and
oom_score_adj. It tells you whether your service was the biggest consumer or just the chosen victim.
Then check the runtime view:
docker inspect -f '{{.State.OOMKilled}} {{.State.ExitCode}}' <container> # 137 = 128 + SIGKILL
kubectl describe pod <pod> # Last State: Terminated, Reason: OOMKilled
Then read the cgroup (v2):
cat memory.max memory.current memory.peak
cat memory.events # counters for high, max, oom, oom_kill: has this happened before?
cat memory.stat # anon vs file vs slab vs shmem
cat memory.pressure # PSI: was it stalling before death?
On cgroup v1 the equivalents are memory.limit_in_bytes, memory.max_usage_in_bytes, memory.failcnt, and memory.oom_control.
After restart, watch the live process: /proc/<pid>/status (VmRSS, VmHWM, RssAnon, RssFile), /proc/<pid>/smaps_rollup, pmap -x, and pidstat -r 1. Add a language heap profiler (tracemalloc, Go pprof, a JVM heap dump, jemalloc profiling) to see who owns the bytes.
Interpret the growth shape:
| Pattern | Likely cause |
|---|---|
| Steady climb independent of traffic | Unbounded cache or leak |
| Sharp spikes on big requests | Parsing amplification (a DOM parse can use 5-10x the payload size) |
| High RSS, small heap profile | Allocator fragmentation or native memory |
2. Mitigation without adding RAM
Application level
- Bound the cache by bytes, not entries. Use LRU or W-TinyLFU with a byte budget (for example about 40% of
memory.max) plus a TTL. - Make entries cheaper. Store compact serialized or compressed values rather than parsed object graphs, or move the cache out of process (Redis sidecar or shared tier).
- Use reclaimable memory. Memory-mapped files are page cache that the kernel can evict under pressure instead of killing the process.
- Fix payload handling. Stream-parse JSON, reject oversized bodies by checking
Content-Lengthearly, and cap concurrent large requests. An in-flight-bytes budget that returns 429/503 withRetry-Afteris a good load-shedding mechanism. - Make the runtime container-aware. Set the heap ceiling below the cgroup limit to leave room for stacks, native buffers, and allocator overhead (
-XX:MaxRAMPercentage,GOMEMLIMIT). TuneMALLOC_ARENA_MAXor switch to jemalloc if fragmentation is the issue.
OS and cgroup level
- Set
memory.highbelowmemory.max. The kernel then throttles and reclaims before killing, which gives the app time to shed load. - Use
memory.oom.group=1so the whole container dies cleanly rather than leaving a half-working process. - In Kubernetes, set requests equal to limits (Guaranteed QoS gets a favorable
oom_score_adj), configurekube-reservedandsystem-reserved, and set kubelet eviction thresholds so pods are evicted gracefully before the kernel intervenes. - Be careful with
ulimit -v(RLIMIT_AS). It limits virtual size, not resident memory, and causes spuriousENOMEMin runtimes that reserve large address ranges.
3. RSS vs VSZ
- VSZ is the total virtual address space mapped: reserved but untouched heap, thread stack reservations, shared libraries, and mmapped files. Because Linux overcommits, VSZ can far exceed physical RAM. A 20 GB VSZ on a Go or JVM process is normal and harmless by itself.
- RSS is the portion actually resident in physical RAM. The kernel's OOM scoring is driven mainly by RSS plus swap and page tables, so RSS is what gets you killed.
Caveats:
- RSS counts shared pages in full for every process that maps them. PSS (from
smaps_rollup) divides shared pages proportionally and is better when summing across processes. - The cgroup charge (
memory.current) is not the same as RSS: it also includes page cache, tmpfs, and kernel memory charged to the group.
Debugging signals:
- Rising
RssAnon: cache or parsing is the problem. - Rising VSZ with flat RSS: reservations or mappings, usually benign.
- Rising
RssFile: mmap or page cache, which is reclaimable.
Common mistakes
- Answering "raise the limit" as the fix.
- Treating VSZ as real memory usage.
- Not separating container OOMs from host OOMs.
- Assuming the victim is always the largest consumer in a global OOM.
- Setting the heap limit equal to the container limit.
- Forgetting exit code 137 and the history in
memory.events.
Likely follow-ups
- Proactive cgroup use: hard
memory.maxon every container, a lowermemory.highfor early throttling, andmemory.min/memory.lowto protect critical services. - Load shedding under bursts: a semaphore weighted by request size, rejecting work when memory pressure (
memory.current / memory.maxor PSI) crosses a threshold. - Proving the fix: replay large payloads in a staging container with the same limit and confirm
memory.peakstays below it andoom_killstays at zero.