Quick revision
The short answer
Exit code 137 means the process received SIGKILL, and in Kubernetes that is usually the kernel OOM killer enforcing the container's memory limit, which is why there is no OutOfMemoryError and no heap dump: the JVM never got a chance to react. We confirm it with kubectl describe pod and kernel logs, then compare the process's resident memory with the heap, because the heap is often fine and the growth is in native memory such as threads, metaspace or direct buffers, which native memory tracking can show.
What to remember
In this answer, we saw that exit code 137 is 128 plus SIGKILL, and in Kubernetes it usually means the kernel OOM killer enforced the container memory limit, which is why there is no OutOfMemoryError and no heap dump. We confirmed it with kubectl describe pod and kernel logs, saw that the heap can be healthy while total RSS exceeds the limit because of threads, metaspace, code cache, GC structures, direct buffers and native libraries, and used native memory tracking with a baseline and summary.diff to find the growing area. The fixes are to stop the native growth, lower the heap share or raise the limit; exit-on-OOM flags cannot help because the JVM never sees an error.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Exit code 137 means the process received SIGKILL, and in Kubernetes that is usually the kernel OOM killer enforcing the container's memory limit, which is why there is no OutOfMemoryError and no heap dump: the JVM never got a chance to react. We confirm it with kubectl describe pod and kernel logs, then compare the process's resident memory with the heap, because the heap is often fine and the growth is in native memory such as threads, metaspace or direct buffers, which native memory tracking can show.
Why this question is asked
A pod that restarts every few hours with nothing in the logs is one of the most confusing problems a Java team meets in Kubernetes. Everyone looks for an OutOfMemoryError that never comes, and the heap dashboards look healthy. Interviewers ask this question because the answer requires understanding that the JVM's memory is much more than its heap, and that a container limit is enforced by the kernel, not by Java. In this answer, we will decode exit code 137, confirm the cause, see why the heap may be fine, measure native memory, and choose a fix.
1What Exit Code 137 Means
When a process is killed by a signal, the shell and container runtimes report its exit code as 128 plus the signal number. SIGKILL is signal 9, so 128 + 9 = 137. SIGKILL cannot be caught or handled, so the JVM does not run shutdown hooks, does not write a heap dump and does not log anything. It simply stops.
In Kubernetes, the most common sender of SIGKILL is the Linux kernel's OOM killer. Each container runs in a control group, a cgroup, with a memory limit taken from resources.limits.memory. When the total memory charged to that cgroup reaches the limit and the kernel cannot reclaim enough, it kills a process in it. For a Java container, that is the JVM. A second possible source is the kubelet itself, which sends SIGKILL to a container that ignored SIGTERM for the whole grace period, for example after failing its liveness probe, so we confirm which one it was.
2Confirming the OOM Kill
The pod's status tells us why the last container stopped.
Reason OOMKilled together with exit code 137 settles it. If the reason were Error with 137 and the events showed liveness probe failures, the story would be different: the JVM was stuck or slow to stop, and we would look at thread dumps and probe settings instead. The kernel log line also shows the process's resident memory at the moment of the kill, here about 2 GB, exactly the limit.
3Why the Heap Can Look Fine
The container limit counts all memory the process uses, its resident set size or RSS, the physical memory pages actually in use. The heap is only one part of that. If we run with -XX:MaxRAMPercentage=75 in a 2 GiB container, the heap may use up to 1.5 GiB, which leaves only 512 MiB for everything else the JVM needs. The JVM itself does not count those other areas against any single limit.
So the heap dashboard can show 1.1 GiB used of 1.5 GiB, perfectly healthy, while RSS climbs to 2 GiB because of native memory. Where does that memory go? Every platform thread has its own stack, reserved at about 1 MB and committed as it is used, so a service that grows from 200 to 400 threads can add a hundred megabytes or more without touching the heap. Every loaded class needs metadata in metaspace, the JIT keeps compiled code in a code cache that can reach about 240 MB, and G1 keeps its own bookkeeping, a few percent of the heap, outside the heap.
The areas that most often grow without limit are the last two. Direct buffers, used heavily by Netty and NIO clients, keep their contents in native memory, so a library that holds more and more of them grows RSS while the heap stays flat. And native libraries allocate with malloc on their own, where the C allocator can also keep freed memory in its arenas instead of returning it. The JVM does not know about this memory at all, which is why it never triggers an OutOfMemoryError.
| Area | Example size | Limited by |
|---|---|---|
| Java heap | 1,536 MiB | MaxRAMPercentage=75 |
| Metaspace and class space | 180 MiB | MaxMetaspaceSize, if set |
| Thread stacks (300 threads) | 120 MiB committed | thread count, -Xss |
| Code cache | 70 MiB | ReservedCodeCacheSize |
| GC structures | 60 MiB | collector and heap size |
| Direct buffers | 150 MiB and growing | MaxDirectMemorySize |
| Native libraries, malloc | unknown to the JVM | nothing |
4Measuring Native Memory Over Time
To find which area grows, we enable native memory tracking, NMT, a JVM feature that accounts for the JVM's own native allocations by category. It must be enabled at startup, so we add the flag to the deployment, ideally on one or two pods first, since it has some overhead.
We compare three numbers over time: RSS from the kernel, the committed total from NMT, and the heap. If NMT's growth explains RSS growth, the diff points at the category: Thread growth means more threads are being created, Other growth often means direct buffers, Class growth means metaspace. If RSS grows but NMT does not, the growth is outside what the JVM tracks, such as a native library's own allocations or allocator fragmentation, and we use tools like pmap on the process or the library's own metrics.
5Fixing It
The fix depends on what we found, and there are three levers. First, fix the native growth itself: bound a thread pool that was creating threads without limit, release or pool direct buffers, cap them with -XX:MaxDirectMemorySize so a leak becomes a visible Java error instead of a silent kill, or fix the native library. Second, reduce the heap share so there is room for native memory, for example MaxRAMPercentage=60 or 65 instead of 75 for a service with many threads and heavy I/O. Third, raise the container limit when the measured, stable native footprint is legitimately larger than the budget.
One thing that does not help is -XX:+ExitOnOutOfMemoryError or HeapDumpOnOutOfMemoryError. Both act only when the JVM throws an OutOfMemoryError, and here none occurs: the kernel kills the process from outside before the JVM knows anything is wrong. What does help afterwards is an alert on RSS approaching the limit, for example container working set above 90 percent of the limit, so the next occurrence is caught while there is still something to inspect.
- kubectl describe podLast State, Reason, Exit Code
- Reason is OOMKilled?YesMemory limit hit, continueNoCheck liveness probe and SIGTERM handling
- Compare RSS with heapheap fine, RSS at limit?
- NMT baseline and summary.diffwhich category grows?
- Does NMT explain the growth?YesFix that area, threads, buffers or metaspaceNoLook outside the JVM, native libraries, malloc
- Adjust heap share or limitand alert on RSS near the limit
6 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not set the heap to 90 percent of the container limit to use the memory well?
Because the JVM always needs native memory beyond the heap, and with 90 percent only about 200 MB of a 2 GiB container would be left for threads, metaspace, code cache, GC structures and buffers. Many services need more than that, so they would be OOM killed under load. Around 60 to 75 percent is a common starting point, adjusted after measuring the real native footprint.
Does the page cache count toward the limit?
Pages cached for files the process reads or writes are charged to the cgroup, but the kernel can usually reclaim clean cache before invoking the OOM killer. So a high memory figure that includes cache is not necessarily a problem; the working set metric, which excludes easily reclaimable cache, is the better one to alert on.
Can the JVM see the container limit?
Yes. Modern JVMs are container-aware and read the cgroup limit to size the heap and the number of CPUs. That is why MaxRAMPercentage is relative to the container limit rather than to the host's memory.
7Conclusion
In this answer, we saw that exit code 137 is 128 plus SIGKILL, and in Kubernetes it usually means the kernel OOM killer enforced the container memory limit, which is why there is no OutOfMemoryError and no heap dump. We confirmed it with kubectl describe pod and kernel logs, saw that the heap can be healthy while total RSS exceeds the limit because of threads, metaspace, code cache, GC structures, direct buffers and native libraries, and used native memory tracking with a baseline and summary.diff to find the growing area. The fixes are to stop the native growth, lower the heap share or raise the limit; exit-on-OOM flags cannot help because the JVM never sees an error.
Open with
Exit code 137 is 128 plus 9, so the JVM was killed with SIGKILL, which it cannot catch, and that is why there is no OutOfMemoryError, no shutdown hook and no heap dump.
Then, if they want more
In Kubernetes that is almost always the kernel OOM killer enforcing the container memory limit, so first I run kubectl describe pod and look for Last State Terminated, Reason OOMKilled, and check the kernel log for the cgroup out of memory line.
If the reason were not OOMKilled, I would check whether a failed liveness probe led to a kill after the grace period. Then I compare RSS with the heap.
Often the heap is fine, say 1.1 of 1.5 gigabytes, but RSS hits the 2 gigabyte limit because the container also has to fit thread stacks, metaspace, code cache, GC structures, direct buffers and native libraries.
To see which part grows, I enable native memory tracking at startup on a pod or two, take a baseline with jcmd VM.native_memory baseline, and a few hours later run summary.diff. If Thread grows, something creates threads without bound; if Other grows, usually direct buffers. If NMT does not explain RSS, it is a native library or allocator fragmentation.
The fix is to stop that growth, cap direct memory, lower MaxRAMPercentage, or raise the limit. ExitOnOutOfMemoryError does not help, because the JVM never throws anything.
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.
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 pod keeps restarting with exit code 137. There is no OutOfMemoryError in the...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/67a8d0da9f/