Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A container with a 2 GiB memory limit is OOM-killed by the kernel. Heap usage was about 1 GiB of -Xmx1200m. The application has around 1,500 platform threads. Walk through where the rest of the memory went and what you would change.

The kernel counts every byte the process touches, not just the heap. With 1,500 platform threads, the touched part of their stacks alone can be several hundred megabytes, and metaspace, the code cache, GC structures, direct buffers and glibc malloc arenas add the rest until the 2 GiB limit is crossed. We confirm it with native memory tracking, then cut the thread count with bounded pools or virtual threads and size the heap as a percentage of the limit so non-heap memory has explicit room.

Why this question is asked

Kernel OOM kills are among the most confusing failures in Java on Kubernetes, because the application logs nothing: the heap looked fine, no OutOfMemoryError appeared, and the pod simply restarted. Interviewers ask this to check that we know the JVM is a process with many memory areas, and that we can account for them with real tools. In this answer, we will recognise the kill, add up where 2 GiB goes, measure it with native memory tracking, look at the thread stacks in detail, and then make the changes that keep the process inside its limit.

1Recognising a Kernel OOM Kill

When a container exceeds its memory limit, the Linux kernel's OOM killer sends SIGKILL to the process. The JVM gets no chance to react, so there is no Java exception and no heap dump. What we see instead is the exit code 137, which is 128 plus signal 9, and in Kubernetes the container's last state shows the reason OOMKilled.

Shell
kubectl describe pod orders-7f9c
# Last State: Terminated
# Reason: OOMKilled
# Exit Code: 137
# Restart Count: 6

That is the first fact to establish. If the logs showed java.lang.OutOfMemoryError, the heap or another Java-managed area hit its own limit. Here the process as a whole crossed the container limit.

2Adding Up Where 2 GiB Goes

The heap is capped at 1,200 MB, and although only about 1 GiB was in use, G1 may have committed and touched most of the 1,200 MB, and the kernel counts touched pages. That leaves roughly 850 MB for everything else. A realistic budget for this service shows how quickly it disappears.

A plausible picture of the process at the moment of the kill
AreaApproximate resident sizeWhy
Java heap (committed)1,150 MB-Xmx1200m, mostly touched by G1
Thread stacks300 MB1,500 threads x about 200 KB actually touched
Metaspace and class space180 MBSpring, Hibernate, many generated classes
Code cache80 MBJIT-compiled code
GC structures60 MBG1 remembered sets and marking bitmaps
Direct buffers60 MBNetty or NIO clients
malloc arenas and other native200 MBglibc per-thread arenas, libraries
Totalabout 2,030 MBover the 2,048 MiB limit at peak
Illustrative numbers; the real ones come from native memory tracking.

3Measuring It With Native Memory Tracking

We do not have to guess. HotSpot's native memory tracking, NMT, records how much memory the JVM itself has reserved and committed in each category. It must be switched on at startup, costs a few percent of performance, and then can be read at any time with jcmd.

Shell
java -XX:NativeMemoryTracking=summary -Xmx1200m -jar orders.jar
jcmd 1 VM.native_memory summary
# Total: reserved=3610MB, committed=1985MB
# - Java Heap (reserved=1200MB, committed=1200MB)
# - Class (reserved=1090MB, committed=182MB)
# - Thread (reserved=1530MB, committed=310MB) <- 1,500 threads
# - Code (reserved=250MB, committed=81MB)
# - GC (reserved=95MB, committed=62MB)
# - Internal (reserved=8MB, committed=8MB)
# - Other (reserved=60MB, committed=60MB) <- direct buffers
jcmd 1 VM.native_memory baseline # take a baseline now
jcmd 1 VM.native_memory summary.diff # later: what grew since the baseline

Notice the Thread line: the reserved size is huge, 1 MB per thread, but only the committed part counts against the limit. Comparing committed totals with the container's resident memory also tells us how much lives outside the JVM's own accounting, such as malloc arenas and native libraries.

4Why 1,500 Threads Cost So Much

Each platform thread reserves a stack, 1 MB by default on 64-bit Linux. The reservation itself is free; what costs memory is the pages the thread actually touches as its call chain grows. A thread running a Spring MVC request through filters, proxies, Hibernate and a JDBC driver can easily touch 100 to 300 KB, and the pages stay resident after the call returns. Multiply by 1,500 and we have 150 to 450 MB.

Threads also bring a second, less visible cost. glibc's malloc gives threads separate arenas to avoid lock contention, and on 64-bit systems it may create up to eight arenas per CPU core, each reserving 64 MB of address space and keeping freed memory for reuse. With many threads allocating native memory, arenas can add hundreds of megabytes of resident memory that NMT does not see.

5What We Change

The biggest lever is the number of threads. First we find where they come from with a thread dump; names like http-nio-8080-exec-, pool-37-thread- or HikariPool tell us which pool owns them. Then we bound the pools: an HTTP server rarely needs more than a couple of hundred worker threads, and an unbounded cached thread pool should become a fixed one. For blocking I/O, virtual threads remove the need for large pools altogether; in Spring Boot 3.2 and later, spring.threads.virtual.enabled=true runs request handling on them.

Then we leave explicit headroom. Instead of a fixed -Xmx that someone picked once, we size the heap as a percentage of the container limit, such as 60 percent, and leave the rest to non-heap areas we have measured. Guard limits make leaks fail with a Java error instead of a kill.

Text
# Before
-Xmx1200m # 59% of the limit, 1,500 threads
# After
-XX:MaxRAMPercentage=60 # 1,228 MB of 2,048 MiB, scales with the limit
-XX:MaxMetaspaceSize=256m
-XX:MaxDirectMemorySize=128m
-XX:NativeMemoryTracking=summary # keep it on while we tune
MALLOC_ARENA_MAX=2 # environment variable for glibc
spring.threads.virtual.enabled=true # request handling on virtual threads
# Result after a week: about 120 platform threads, resident memory steady near 1.6 GiB

After the change, we watch the container's working set against its limit for several days. If it still creeps up, NMT's summary.diff shows which category is growing.

6Follow-up questions

How does glibc's per-thread malloc arena behaviour add to this, and what can you set?

glibc creates extra malloc arenas so that threads allocating native memory at the same time do not fight over one lock. On 64-bit Linux it allows up to eight arenas per core, each reserving 64 MB and holding on to freed chunks for reuse, so a process with many threads can carry hundreds of megabytes of resident memory that is free from the program's point of view. Setting the environment variable MALLOC_ARENA_MAX to 2 or 4 caps the number of arenas, usually with no measurable slowdown for Java services. Alternatives are switching to jemalloc, or asking glibc to give memory back with jcmd <pid> System.trim_native_heap on recent JDKs.

Why does the kernel kill the process instead of the JVM throwing OutOfMemoryError?

Because the limit belongs to the container, not to the JVM. The JVM only enforces its own limits, such as the maximum heap or MaxMetaspaceSize. When the total resident memory of the process crosses the cgroup limit, the kernel stops it with SIGKILL, which cannot be caught.

Is it enough to lower the heap?

It buys room but does not fix the cause. If the thread count keeps growing, any headroom will be used up eventually. Lower the heap only after the non-heap areas are measured and bounded.

7Conclusion

In this answer, we saw that a container OOM kill with exit code 137 and no Java error means the whole process crossed its limit. With a 1,200 MB heap, the remaining 850 MB went to thread stacks for 1,500 threads, metaspace, the code cache, GC structures, direct buffers and malloc arenas. Native memory tracking with jcmd VM.native_memory summary shows the JVM's share, and we fix it by cutting the thread count with bounded pools or virtual threads and sizing the heap as a percentage of the limit with guard limits on the rest.

What to say in the interviewAbout 80 seconds aloud

Open with

Exit code 137 with OOMKilled and nothing in the Java logs tells me the kernel killed the process for crossing the cgroup limit, so I stop looking only at the heap.

Then, if they want more

The heap is capped at 1,200 MB and G1 has probably touched most of it, which leaves about 850 MB for everything else. 1,500 platform threads each reserve a 1 MB stack, and the pages they actually touch, often 100 to 300 KB in a Spring request, stay resident, so that alone can be a few hundred megabytes.

On top of that come metaspace, the code cache, G1's own structures, direct buffers, and glibc malloc arenas, which can be large with many threads.

I would confirm it by starting with NativeMemoryTracking set to summary and running jcmd VM.native_memory summary, which breaks committed memory into Thread, Class, Code, GC, Internal and Other, and I would compare that total with the container's resident memory to see what is outside the JVM.

Then I would cut the thread count: find the pools in a thread dump, bound them, and use virtual threads for blocking I/O. I would size the heap with MaxRAMPercentage, around 60 percent, add MaxMetaspaceSize and MaxDirectMemorySize as guards, set MALLOC_ARENA_MAX, and watch the working set for a week.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.2
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 container with a 2 GiB memory limit is OOM-killed by the kernel. Heap usage was about...": https://scrivio-interview-library.netlify.app/java/memory-layout/b2f6034e2c/