Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A Java pod restarts several times a day with exit code 137 and reason OOMKilled, but heap usage in metrics never exceeds 60 percent of -Xmx. How would you investigate?

Exit code 137 means the process was killed with SIGKILL, and the reason OOMKilled says the kernel did it because the container's total memory went over its limit; a Java heap OutOfMemoryError looks completely different. So the heap is not the problem: something outside it is. I would compare the process's resident memory with the heap, turn on Native Memory Tracking to see which JVM area grows, look at the usual suspects (threads, direct buffers, metaspace, glibc malloc arenas, mapped files) and then cap that area or lower the heap to leave a real margin.

Why this question is asked

This is a classic production puzzle, and interviewers like it because the obvious data, a heap graph that looks healthy, points the wrong way. Solving it requires knowing how the kernel counts memory, what the JVM uses outside the heap, and which tools show it. In this answer, we will decode exit code 137, measure total memory against heap, use Native Memory Tracking, walk through the common causes, and then apply fixes and a safety margin.

1What Exit Code 137 Tells Us

When a process is killed by a signal, the exit code is 128 plus the signal number. SIGKILL is signal 9, so 137 means the process received SIGKILL. A SIGKILL cannot be caught or handled, so the JVM had no chance to log anything. The reason OOMKilled, which we see with kubectl describe pod under Last State, says who sent it: the kernel's OOM killer, because the memory charged to the container's cgroup exceeded the limit.

Compare this with a Java heap problem. If the heap fills, the JVM throws java.lang.OutOfMemoryError: Java heap space inside the application. It appears in the logs, and the process exits only if we told it to, for example with -XX:+ExitOnOutOfMemoryError, usually with exit code 3 rather than 137. So the heap staying at 60 percent of -Xmx is consistent with the evidence: the heap never filled. The memory that pushed the container over the limit is somewhere else.

Shell
$ kubectl describe pod orders-7d9c-xk2lp
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Restart Count: 6
Limits:
memory: 2Gi

2Measure Total Memory Against the Heap

The kernel counts the resident set size (RSS), the memory pages the process actually uses, plus some page cache, against the cgroup. So the first step is to graph container memory, for example container_memory_working_set_bytes from cAdvisor, next to the JVM's heap used and committed from Micrometer. If container memory climbs to 2 GiB while the heap stays around 900 MiB, the gap is non-heap memory, and its shape tells us a lot: a steady climb suggests a leak, a step after a traffic burst suggests buffers or threads.

Notice that the heap's committed size matters more than the used size here. The JVM commits heap memory as it grows and often does not give it back, so the heap's footprint is close to -Xmx after some hours even if used heap is 60 percent.

3Native Memory Tracking

Native Memory Tracking (NMT) is built into the JVM and breaks its memory down by area. We start the JVM with -XX:NativeMemoryTracking=summary, which costs a few percent of performance, and query it with jcmd. Taking a baseline and a later diff shows which area grows.

Shell
$ kubectl exec orders-7d9c-xk2lp -- jcmd 1 VM.native_memory baseline
$ # ... a few hours later
$ kubectl exec orders-7d9c-xk2lp -- jcmd 1 VM.native_memory summary.diff
Total: reserved=3.9GB +410MB, committed=1.9GB +405MB
- Java Heap (reserved=1228MB, committed=1228MB +0MB)
- Class (reserved=1.1GB, committed=160MB +2MB)
- Thread (reserved=420MB, committed=420MB +210MB) # 210 new threads
- Other (reserved=380MB, committed=380MB +190MB) # direct buffers
- Code (reserved=250MB, committed=70MB +3MB)
Step throughInvestigating an OOMKilled JVM
  1. Confirm 137 and OOMKilledkernel kill, not a Java heap error
  2. Graph container memory vs heap committedfind the size and shape of the gap
  3. Enable NMT and take a baselinejcmd VM.native_memory summary.diff
  4. NMT total grows with RSS?
    YesFind the growing JVM areaNoLook outside NMT: malloc arenas, native libs, mapped files
  5. Cap or fix the growing areathreads, direct memory, metaspace
  6. Lower heap share and keep a margine.g. 15 to 25 percent of the limit

6 steps in order. Play them, or step through them one at a time.

The heap graph rules out one cause; the gap between RSS and heap points at the rest.

In this example, two areas grew: Thread, because the number of threads doubled, and Other, which includes direct ByteBuffers. If NMT's committed total stays flat but RSS keeps growing, the memory is allocated outside the JVM's own tracking, by native libraries or by the C library's allocator, which points at malloc arenas or a native library leak.

4The Usual Suspects

Threads are the first. Every thread has a stack, up to 1 MiB by default, plus native bookkeeping. An unbounded thread pool, an executor created per request, or a client library with its own pool per instance can add hundreds of threads under load. A thread dump with jcmd 1 Thread.print, or the jvm.threads.live metric, shows it.

Direct buffers are the second. Netty, used by WebFlux, Reactor Netty, gRPC and many clients, allocates direct memory outside the heap. By default the JVM allows direct memory up to the max heap size, so with a 1.2 GiB heap, direct memory can add another 1.2 GiB. Netty also has its own limit settings, so check your version and library.

Metaspace grows with loaded classes. It is normally stable, but a classloader leak, for example from repeated dynamic proxy generation or scripting engines, makes it climb. glibc malloc arenas are a subtler cause: the C allocator creates up to eight arenas per CPU core to reduce contention, and each holds freed memory, so a JVM with many threads calling native code can carry hundreds of megabytes of fragmentation. Memory-mapped files, used by some databases, search libraries and Kafka clients, add RSS too.

5Fixes and a Safety Margin

Once we know the area, the fix follows. For threads, bound the pools and reuse clients, or move blocking work to virtual threads, whose stacks live on the heap and only take memory while used. For direct buffers, cap them with -XX:MaxDirectMemorySize and size Netty's pools. For metaspace, find the leak, and set -XX:MaxMetaspaceSize so a leak fails as an OutOfMemoryError we can see rather than an OOMKill. For malloc, setting the environment variable MALLOC_ARENA_MAX=2 often removes hundreds of megabytes; some teams switch to jemalloc.

Finally, lower the heap share. If the heap is at 85 percent of the limit, there is no room for normal non-heap use. A common target is a heap of 60 to 75 percent of the limit, which leaves 500 MiB or more of a 2 GiB container for everything else, and then we check with NMT that the measured non-heap use fits inside that margin with room to spare.

YAML
env:
- name: JAVA_TOOL_OPTIONS
value: >-
-XX:MaxRAMPercentage=65
-XX:MaxDirectMemorySize=192m
-XX:MaxMetaspaceSize=256m
-XX:NativeMemoryTracking=summary
- name: MALLOC_ARENA_MAX
value: "2"
# heap 1.3 GiB + direct 192 MiB + metaspace 256 MiB + threads and code = under 2 GiB

6Follow-up questions

Why does the OOM killer not trigger a heap dump?

Because the heap dump on OutOfMemoryError is a JVM feature, and the JVM never sees the kill. SIGKILL stops the process immediately. To get evidence, we need to collect NMT snapshots or memory metrics before the kill happens.

Could the page cache cause an OOMKill?

Page cache charged to the cgroup is usually reclaimable, so the kernel frees it before killing. But writing large files quickly, or using memory-mapped files heavily, can add pressure. Working set metrics exclude part of the cache and are a better signal than total usage.

Should we just raise the memory limit?

As a temporary measure, yes, if pods are dying. But without knowing the cause, a leak will reach the new limit too, only later. Raising the limit while the investigation runs buys time; the real fix is to find which area grows.

7Conclusion

In this answer, we saw that exit code 137 with OOMKilled means the kernel killed the JVM for exceeding the container limit, which a healthy heap graph does not rule out. We compared container memory with committed heap, used Native Memory Tracking to find the growing area, went through the common causes of threads, direct buffers, metaspace, malloc arenas and mapped files, and fixed them with caps such as MaxDirectMemorySize, bounded thread pools, MALLOC_ARENA_MAX and a lower heap percentage that leaves a real margin.

What to say in the interviewAbout 90 seconds aloud

Open with

Exit code 137 is 128 plus 9, so the process got SIGKILL, and OOMKilled says the kernel's OOM killer sent it because the container went over its memory limit.

Then, if they want more

That is different from a Java heap OutOfMemoryError, which would be in the logs. So a heap at 60 percent does not contradict anything; the extra memory is outside the heap. I would first graph container working set memory next to the JVM's committed heap.

Committed heap tends to sit near Xmx, and the gap to the container total is non-heap. Then I would enable Native Memory Tracking with NativeMemoryTracking=summary, take a baseline with jcmd VM.native_memory, and later a summary diff, to see which area grows.

The usual causes are too many threads, each with a stack, direct buffers from Netty, which by default can be as large as the heap, metaspace growth from a classloader leak, glibc malloc arenas, and memory-mapped files. If NMT is flat but RSS grows, it is native allocation outside the JVM, and MALLOC_ARENA_MAX=2 often helps.

Then I fix the cause: cap direct memory with MaxDirectMemorySize, bound thread pools, cap metaspace so a leak becomes a visible error. And I lower the heap percentage to around 65 so there is a real margin between max heap and the limit.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Kubernetes documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Java pod restarts several times a day with exit code 137 and reason OOMKilled, but...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/569c80d57c/