You push a Spring Boot app on Tomcat into a 4 GiB PaaS container, set -Xmx3500m because “leave a little for the OS,” and the container gets OOM-killed every couple of hours under load. The JVM logs show heap utilization at 60% right before the kill. The platform shows the container hit its memory limit and got reaped. Both are right — and the gap between them is everything that lives in the JVM process outside the heap.
What’s happening
The JVM heap is one of several memory regions, and -Xmx only caps the heap. A running Tomcat process on JDK 21 consumes:
- Java heap — bounded by
-Xmx. - Metaspace — class metadata. Unbounded by default. A Spring app with many beans + reflection + dynamic proxies can pin 256-512 MiB here.
- Code cache — JIT-compiled native code. Default
ReservedCodeCacheSizeis 240 MiB. - Direct buffers — NIO, Netty, gRPC. Bounded by
-XX:MaxDirectMemorySize, defaults to ≈-Xmx. - Thread stacks —
ThreadStackSize× number of threads. 200 threads × 1 MiB = 200 MiB. - GC overhead, JNI, mmap’d JARs, native libraries (zip, zstd, libssl) — another 100-300 MiB.
- Container overhead — Tomcat itself, agents, the entrypoint shell, log shippers running as sidecars in the same cgroup.
If you set -Xmx3500m in a 4096 MiB container, you’ve allocated 500 MiB for everything that isn’t heap. A real Tomcat workload needs closer to 1000-1400 MiB of non-heap. The kernel kills the container before the JVM ever sees pressure, because cgroup accounting is process-RSS, not Java’s view of its own heap.
The fix is to size -Xmx as a fraction of the container limit and explicitly cap the other regions so the JVM stays inside the cgroup.
How to verify
From inside the running container:
# What the JVM thinks the container limit is
jcmd 1 VM.flags | tr ' ' '\n' | grep -E 'MaxHeap|MaxDirect|MaxMetaspace|ReservedCodeCache'
# Actual RSS vs. cgroup limit
cat /sys/fs/cgroup/memory.max
cat /sys/fs/cgroup/memory.current
# Native memory breakdown (requires -XX:NativeMemoryTracking=summary)
jcmd 1 VM.native_memory summary
VM.native_memory summary is the diagnostic that ends arguments. It shows committed memory per region — heap, metaspace, code, GC, thread, internal, symbol, native memory tracking itself. Sum the committed column and compare to memory.current. If they match within ~100 MiB, your JVM accounting is real. If memory.current is 800 MiB higher than NMT committed, you have native leaks (typically Netty direct buffers or a JNI library) and -Xmx is the wrong knob.
The platform side:
# OOM kills in the last 24h on a Jelastic container
journalctl --since '24 hours ago' | grep -iE 'killed process|oom-killer|memory cgroup'
# Or from the host's perspective if you have it:
dmesg -T | grep -iE 'killed process .*tomcat|out of memory'
A Killed process ... tomcat ... anon-rss:3892MiB line confirms the kernel pulled the trigger, not the JVM throwing OutOfMemoryError.
The fix
Three changes. They compose.
1. Stop hard-coding -Xmx. Use percentages.
JDK 17+ honours -XX:MaxRAMPercentage and reads the cgroup limit correctly. Set it once and the JVM resizes when the container resizes:
JAVA_OPTS="\
-XX:+UseContainerSupport \
-XX:InitialRAMPercentage=60 \
-XX:MaxRAMPercentage=70 \
-XX:MaxDirectMemorySize=256m \
-XX:MaxMetaspaceSize=384m \
-XX:ReservedCodeCacheSize=192m \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-XX:+ParallelRefProcEnabled \
-XX:+ExitOnOutOfMemoryError \
-Xss512k \
-XX:NativeMemoryTracking=summary \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/var/log/tomcat/heap-dump.hprof"
MaxRAMPercentage=70 gives heap 70% of the container, leaving 30% for everything else. On a 4 GiB container that’s ~2.8 GiB heap, ~1.2 GiB for metaspace + direct + code + threads + native + Tomcat overhead. Tight but realistic. If you’re getting OOMs at 70%, drop to 60% and look at what’s eating the rest before pushing further.
2. Cap thread stack size and the connector thread pool.
A default Tomcat sets maxThreads=200 on its connector. With 1 MiB stacks that’s 200 MiB before a single request hits. -Xss512k cuts that in half. In conf/server.xml:
<Connector port="8080"
protocol="org.apache.coyote.http11.Http11Nio2Protocol"
connectionTimeout="20000"
maxThreads="100"
minSpareThreads="10"
acceptCount="100"
maxConnections="2000"
compression="on"
compressibleMimeType="text/html,text/css,application/javascript,application/json"
URIEncoding="UTF-8"
redirectPort="8443" />
100 threads is a reasonable starting point for a Spring service that does mostly I/O. Use maxConnections="2000" if you’re behind a reverse proxy that keeps connections open. If your service is CPU-bound, lower maxThreads further — adding threads beyond cores hurts throughput.
3. Dockerfile pattern that survives a memory limit change.
If you build your own image, this is the production-tested base. Same image on a 2 GiB or 8 GiB container — the JVM reads the cgroup at startup.
FROM eclipse-temurin:21-jre-alpine AS runtime
RUN apk add --no-cache curl tini bash \
&& addgroup -S app -g 1001 \
&& adduser -S app -G app -u 1001 -h /app
WORKDIR /app
USER app
ENV CATALINA_HOME=/opt/tomcat \
CATALINA_BASE=/app \
JAVA_OPTS="-XX:+UseContainerSupport \
-XX:MaxRAMPercentage=70 \
-XX:InitialRAMPercentage=60 \
-XX:MaxDirectMemorySize=256m \
-XX:MaxMetaspaceSize=384m \
-XX:ReservedCodeCacheSize=192m \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-Xss512k \
-XX:+ExitOnOutOfMemoryError \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/app/logs/heap-dump.hprof \
-XX:NativeMemoryTracking=summary \
-Djava.awt.headless=true \
-Djava.net.preferIPv4Stack=true"
COPY --chown=app:app tomcat/ /opt/tomcat/
COPY --chown=app:app webapps/ /app/webapps/
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=40s --retries=3 \
CMD curl -fsS http://127.0.0.1:8080/actuator/health/liveness || exit 1
ENTRYPOINT ["/sbin/tini", "--", "/opt/tomcat/bin/catalina.sh", "run"]
Two things this gets right that most production Dockerfiles miss: tini reaps zombie children from the JVM (a Spring app that shells out to imagemagick will leak processes without it), and ExitOnOutOfMemoryError plus the heap-dump options mean the platform’s restart loop replaces a broken container instead of letting it limp along.
4. Validate the math before redeploying.
Run the container locally with the same limit and watch NMT before traffic:
docker run --rm --memory=4g --memory-swap=4g \
-e JAVA_OPTS="$JAVA_OPTS" \
-p 8080:8080 \
--name tomcat-sizing-check \
myorg/tomcat-app:test
# Wait 60s for warmup, then
docker exec tomcat-sizing-check jcmd 1 VM.native_memory summary | \
awk '/Total/ && /committed/ {print; exit}'
Sum of committed should be 60-80% of 4 GiB at idle. If you’re already at 90% before traffic, drop MaxRAMPercentage or MaxMetaspaceSize until you have headroom. Headroom is what absorbs garbage-collection peaks and Netty buffer spikes.
We run Tomcat and Spring workloads under Cloud PaaS with JVM sizing baked into the deployment pipeline and OOM alerting wired into our monitoring plane. If your containers are getting killed and the heap looks fine, start a sizing review.