Why can a Java container get OOM-killed when the heap isn't full?

November 18, 2026 5 answers
Asked by Veldnaro.8p

My understanding is that the JVM heap is only part of a container's memory use. What else needs room, and how should I set the heap limit without getting caught by container sizing defaults?

Answers

_t48~ferruvani.6

The container limit covers more than the Java heap. The JVM also needs memory for metaspace, thread stacks, compiled code and direct buffers. Native allocations made by libraries can add more. The container can hit its limit even while the heap has space left.

Start with a heap ceiling that leaves room for those costs, then check actual container memory under a representative workload. The right headroom depends on the application and its thread count, not just the heap percentage.

Veldnaro.8p:

So a healthy-looking heap graph alone doesn't tell me how close the container is to its limit?

_c59~mivnexo.3:

Right. Compare it with the container's total memory usage, and look into native and off-heap use as well.

Reply to this answer
8v!quarrymixer_613

For container-aware heap sizing, -XX:MaxRAMPercentage is a useful option, but don't treat one percentage as universal. A heap at 75% of 4 GiB leaves about 1 GiB for everything else. At 75% of 512 MiB, it leaves only 128 MiB, which can be tight.

Also check -Xms as well as -Xmx and percentage settings. A large initial heap can make a simple estimate based only on the default maximum misleading. Verify the effective settings for the actual JDK and flags you deploy.

Reply to this answer
3b+fablewrench_817

Check whether container support is actually enabled. It's enabled by default from JDK 10 onward, but -XX:-UseContainerSupport turns it off. Then heap sizing can be based on host memory rather than the container limit.

For JDK 8, support depends on the build and its backports, so check the specific runtime rather than assuming it behaves like a newer JDK.

Reply to this answer
_n56~plivexo.8

Don't assume the default heap is always 25% of the limit or that a particular garbage collector will be selected. Tests with some JDK 11, 17 and 21 builds found different behavior for small limits. Check the effective heap size and collector with the exact runtime and resource limits you're using.

Reply to this answer
8r!vexluno_735

A pre-deployment budget check can catch an obviously impossible configuration, but it can't see every native allocation a library might make. Use it as a warning, not as the final container size.

Once the app is running, container metrics and JVM Native Memory Tracking can help investigate where memory is going. Check the workload under realistic conditions rather than relying on heap settings alone.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions