Quick revision
The short answer
-Xmx limits only the heap, but the JVM also uses memory outside it: metaspace for class metadata, a stack for every thread, the code cache for JIT-compiled code, direct buffers, garbage collector structures and the allocator's own overhead. A typical Spring Boot service needs 250 to 500 MB of that, so a 1.2 GB limit with a 1 GB heap leaves far too little, and the kernel kills the container when the total crosses the limit. We size the heap with -XX:MaxRAMPercentage instead of a fixed -Xmx, leave 25 percent or more as headroom, and use native memory tracking (-XX:NativeMemoryTracking) to see where the memory actually goes.
What to remember
In this answer, we saw that OOMKilled means the kernel killed the whole JVM process for exceeding the container limit, not that the heap ran out. -Xmx limits only the heap, while metaspace, thread stacks, code cache, direct buffers, GC structures and malloc overhead easily need 250 to 500 MB more, so a 1.2 GB limit for a 1 GB heap leaves too little. We fixed it with -XX:MaxRAMPercentage and 25 percent or more headroom, and measured real usage with -XX:NativeMemoryTracking and jcmd VM.native_memory.
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.
-Xmx limits only the heap, but the JVM also uses memory outside it: metaspace for class metadata, a stack for every thread, the code cache for JIT-compiled code, direct buffers, garbage collector structures and the allocator's own overhead. A typical Spring Boot service needs 250 to 500 MB of that, so a 1.2 GB limit with a 1 GB heap leaves far too little, and the kernel kills the container when the total crosses the limit. We size the heap with -XX:MaxRAMPercentage instead of a fixed -Xmx, leave 25 percent or more as headroom, and use native memory tracking (-XX:NativeMemoryTracking) to see where the memory actually goes.
Why this question is asked
OOMKilled is confusing the first time we see it, because the application logs show no OutOfMemoryError, the heap dashboards look healthy, and the pod simply restarts with exit code 137. Interviewers ask this debugging question because the answer requires knowing that the JVM is much more than its heap. In this answer, we will see what OOMKilled means and how it differs from a Java OutOfMemoryError, list where the non-heap memory goes with typical sizes, do the arithmetic for this pod, fix the sizing, and measure the real usage with native memory tracking.
1What OOMKilled Means
A container's memory limit is enforced by the Linux kernel through cgroups. When the processes in the container try to use more memory than the limit and the kernel cannot reclaim enough, the kernel's OOM killer ends the process with SIGKILL. Kubernetes reports the container as terminated with reason OOMKilled and exit code 137 (128 + 9, the signal number) and restarts it.
This is different from java.lang.OutOfMemoryError. The JVM throws OutOfMemoryError when the heap, or metaspace, or another JVM-managed area, reaches the JVM's own limit; the JVM is still alive and can log it. With OOMKilled, the JVM is under its own limits, the heap is below 1 GB, but the whole process is above what the kernel allows, so it dies instantly with no log line and no heap dump.
2Where the Non-Heap Memory Goes
The JVM process uses memory in several areas besides the heap, and each one exists for a reason. The biggest is usually metaspace, where the JVM keeps the metadata of every loaded class. A Spring Boot application with Hibernate and a few dozen starters loads 15,000 to 25,000 classes, so metaspace commonly needs 100 to 200 MB, and it has no limit by default. Next come thread stacks: every thread gets its own stack, up to 1 MB reserved by default, and a service with 200 Tomcat threads plus pool and GC threads can commit tens of megabytes or more. The JIT compiler then needs the code cache for the machine code it produces, often 50 to 100 MB once the service has warmed up.
The rest is easier to overlook. Direct buffers, used by NIO, Netty and some database drivers, live outside the heap and are capped by default only by the heap size. The garbage collector needs its own structures, such as G1's remembered sets and card tables, often several percent of the heap. And the native allocator, glibc malloc, keeps arenas and fragmentation of its own.
| Area | Typical size | Limited by |
|---|---|---|
| Java heap | 1024 MB | -Xmx1g |
| Metaspace | 150 MB | -XX:MaxMetaspaceSize (unlimited by default) |
| Thread stacks (about 200 threads) | 50 to 200 MB | -Xss and thread count |
| Code cache | 60 MB | -XX:ReservedCodeCacheSize (240 MB default) |
| GC structures | 40 to 80 MB | heap size and collector |
| Direct buffers, malloc, other native | 50 to 150 MB | -XX:MaxDirectMemorySize, arenas |
| Total | about 1.4 to 1.7 GB | container limit is 1.2 GB |
3Doing the Arithmetic for This Pod
With -Xmx1g and a limit of 1.2 GB (about 1229 MB), the non-heap areas have about 200 MB. That is often enough right after start-up, when the heap is still small and the JIT has compiled little, which is why the pod passes its tests. Over the next hours, the heap grows towards its maximum (the JVM rarely gives heap memory back quickly), the code cache fills as more code becomes hot, and a traffic peak adds threads and direct buffers. The total crosses 1.2 GB, and the kernel kills the process, often under load, when it hurts most.
Notice that the heap being "only" 70 percent full does not protect us. Once the heap has expanded to 1 GB, those pages are committed and counted against the container, even if the objects in them are garbage.
4Fix: Size the Heap as a Share of the Limit
The fix is to size the heap relative to the container instead of hard-coding it, and to give the non-heap areas a realistic share. -XX:MaxRAMPercentage sets the maximum heap as a percentage of the memory the JVM sees, which inside a container is the cgroup limit.
A 75 percent heap with 25 percent headroom is a common starting point; services with many threads, heavy use of Netty or many loaded classes may need 65 to 70 percent. If the service really needs a 1 GB heap, the limit should be around 1.4 to 1.5 GB. The percentage also keeps working when someone changes the limit later, which a fixed -Xmx does not. If the thread count is very high, reducing -Xss or capping thread pools is another lever, and capping -XX:MaxDirectMemorySize makes a direct buffer leak fail as a clear Java error instead of an OOMKill.
5Measuring It With Native Memory Tracking
To replace estimates with numbers, we start the JVM with native memory tracking, which makes the JVM account for every area it allocates, at a small cost of a few percent of performance, and then ask it for a summary with jcmd.
Committed is what counts against the container. We take a baseline with jcmd 1 VM.native_memory baseline after start-up and compare later with summary.diff, which shows which area grows over time, for example Other growing steadily points to a direct buffer leak. NMT does not see memory allocated by native libraries outside the JVM's control, nor all of malloc's fragmentation, so if the container's usage (from kubectl top or the cgroup statistics) is well above NMT's committed total, the gap points there, and tuning glibc with MALLOC_ARENA_MAX=2 is a common fix.
6Follow-up questions
Why does the JVM not use the container limit by itself?
It does, partly. Since JDK 10, and in later JDK 8 updates, the JVM reads the cgroup limit and sizes its default heap from it, but the default is only 25 percent. An explicit -Xmx overrides that completely, so the JVM no longer relates the heap to the limit at all. MaxRAMPercentage keeps the relationship and lets us choose the share.
Would -XX:+ExitOnOutOfMemoryError help here?
Not with OOMKilled, because the JVM never raises an OutOfMemoryError; the kernel kills it first. The flag is still useful for real heap exhaustion, since it makes the JVM exit so Kubernetes restarts it, instead of limping along in a broken state.
How do we tell a real leak from bad sizing?
Bad sizing shows a total that rises during warm-up and then levels off close to the limit, so the pod dies after warm-up or at peak load and the restarts follow traffic. A leak shows one NMT area, or the heap after full GCs, growing steadily over hours or days regardless of load. A baseline and summary.diff taken a few hours apart tell the two apart.
7Conclusion
In this answer, we saw that OOMKilled means the kernel killed the whole JVM process for exceeding the container limit, not that the heap ran out. -Xmx limits only the heap, while metaspace, thread stacks, code cache, direct buffers, GC structures and malloc overhead easily need 250 to 500 MB more, so a 1.2 GB limit for a 1 GB heap leaves too little. We fixed it with -XX:MaxRAMPercentage and 25 percent or more headroom, and measured real usage with -XX:NativeMemoryTracking and jcmd VM.native_memory.
Open with
OOMKilled means the Linux kernel killed the process because the container's total memory went over the cgroup limit, which is why there is no OutOfMemoryError in the logs and the exit code is 137. -Xmx only limits the Java heap, and the JVM uses a lot of memory outside it.
Then, if they want more
Metaspace for class metadata is often 100 to 200 megabytes for a Spring application, every thread has a stack, the JIT's code cache is another 50 to 100 megabytes, there are direct buffers for NIO and Netty, garbage collector structures like G1's remembered sets, and malloc overhead.
Together that is often 250 to 500 megabytes, and with a 1 GB heap in a 1.2 GB limit there are only about 200 left. Right after start-up it fits, but as the heap grows to its maximum and the code cache fills under load, the total crosses the limit.
The fix is to stop using a fixed Xmx and use MaxRAMPercentage, around 75, so the heap is a share of the limit and at least a quarter is left for non-heap memory. If the service really needs a gigabyte of heap, the limit should be around 1.5 gigabytes.
To confirm, I would enable NativeMemoryTracking at summary level and run jcmd VM.native_memory summary, taking a baseline and a diff later to see which area grows.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 10, Java 8
- 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
- Spring Boot reference
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 Boot service in Kubernetes is OOMKilled although -Xmx is 1 GB and the memory limit is...": https://scrivio-interview-library.netlify.app/spring-boot/packaging-and-deployment/bcaa772293/