On this page
Short answer
A Tomcat container is killed with exit code 137 when the JVM's total memory, not just its heap, passes the container limit. The JDK reads the limit and sets the heap to 25 percent of it by default. Choose a deliberate -XX:MaxRAMPercentage, budget separately for Metaspace, thread stacks, the code cache and direct buffers, and keep the flags in setenv.sh.
A Tomcat container is killed with exit code 137 when the JVM's total memory, not just its heap, passes the container limit. The JDK reads the limit and sets the heap to 25 percent of it by default. Choose a deliberate -XX:MaxRAMPercentage, budget separately for Metaspace, thread stacks, the code cache and direct buffers, and keep the flags in setenv.sh.
This post is Tomcat-specific. For the generic Java version, see our Java container memory limit review. Our Tomcat consulting engineers use the budget below when a Tomcat container restarts without a Java error in its log.
How much memory does Tomcat use beyond the heap?
The container limit applies to the whole process, so every memory area counts. The table shows each area, what controls it and the JDK 21 default.
| Area | Controlled by | JDK 21 default |
|---|---|---|
| Java heap | -Xmx or -XX:MaxRAMPercentage | 25 percent of detected memory |
| Metaspace | -XX:MaxMetaspaceSize | No limit |
| Thread stacks | -Xss, one per thread | 1024 KB on Linux x64, 2048 KB on Linux AArch64 |
| JIT code cache | -XX:ReservedCodeCacheSize | 240 MB maximum (48 MB without tiered compilation) |
| Direct buffers | -XX:MaxDirectMemorySize | Chosen by the JVM |
Tomcat's default pool of 200 request threads alone can reserve about 200 MB of stack on Linux x64, before the threads for GC, the compiler and your application. This is the part of the budget teams most often forget.
How do I set MaxRAMPercentage for Tomcat?
Raise it deliberately. The JDK enables container detection by default on Linux (-XX:+UseContainerSupport) and sizes the heap from the limit it finds, at 25 percent unless you say otherwise.
- Measure non-heap use first with Native Memory Tracking (see the next section).
- Take the limit and deduct Metaspace, stacks, code cache, direct buffers and a safety margin. What remains is your heap ceiling.
- Convert it to a percentage. For a 2 GiB limit,
-XX:MaxRAMPercentage=75gives about 1.5 GiB of heap and leaves about 0.5 GiB for everything else. Treat that as an illustration to test, not a recommendation. - Add a
-XX:MaxMetaspaceSizeso a class loader leak ends in a Java error you can read. - Put the flags in
CATALINA_BASE/bin/setenv.shasCATALINA_OPTS.
The Tomcat documentation says setenv.sh in CATALINA_BASE wins over the one in CATALINA_HOME, and not to use JAVA_OPTS for memory limits. It also notes the script is ignored when Tomcat runs as a Windows service. More production defaults are in our Tomcat production defaults guide.
Is it exit 137 or a Java OutOfMemoryError?
They have different evidence, so check which one you have before changing a flag.
| Signal | Where you see it | What it means |
|---|---|---|
OutOfMemoryError with a stack | Tomcat log | The JVM hit its own heap or Metaspace limit |
reason: OOMKilled, exitCode: 137 | kubectl describe pod, last state | The container passed its memory limit and was killed; no Java stack |
| Heap dump written | Only with -XX:+HeapDumpOnOutOfMemoryError | Java errors only; the option is off by default and a kernel kill leaves no dump |
The Kubernetes documentation shows a container that exceeds its limit as terminated with reason OOMKilled and exit code 137, and restarted by the kubelet where possible.
How do pod requests and limits relate to the JVM settings?
A pod's memory request is what the scheduler reserves on a node, and its limit is the ceiling the kernel enforces. The JVM reads the limit, not the request, so MaxRAMPercentage is a percentage of the limit. The same applies to a plain Docker container started with a memory limit.
Two habits help. Keep the request equal to the limit for a Tomcat workload so the node is not oversold against memory you expect to use. Watch the container's working set in your monitoring tool, not only the heap, because a slow memory leak outside the heap shows up there first and the kill follows at the limit.
How do I find what is using the non-heap memory?
- Start the JVM with
-XX:NativeMemoryTracking=summary. The default is off. - Run
jcmd <pid> VM.native_memory baselineafter warm-up. - Later run
jcmd <pid> VM.native_memory summary.diffto see which subsystem grew since the baseline. - For classes, use
jcmd <pid> VM.metaspace; for threads, count them withjcmd <pid> Thread.print.
AceMQ's senior engineers review Tomcat memory budgets with your team, and Tomcat support runs 24/7 with response targets from 15 minutes, by plan.
Sources
- docs.oracle.com/en/java/javase/21/docs/specs/man/java.html, read 3 December 2026.
- docs.oracle.com/en/java/javase/21/docs/specs/man/jcmd.html, read 3 December 2026.
- tomcat.apache.org/tomcat-10.1-doc/RUNNING.txt, read 3 December 2026.
- tomcat.apache.org/tomcat-10.1-doc/config/http.html, read 3 December 2026.
- kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/, read 3 December 2026.
Frequently Asked Questions
What is the default heap size for Tomcat in a container?
The JDK sets the maximum heap to 25 percent of the detected memory by default (MaxRAMPercentage). On a 2 GiB container limit that is about 512 MiB, which is often too small for the application and still not a guarantee against a kill.
Why does my Tomcat pod show OOMKilled when the heap is below the limit?
The heap is only part of the process. A heap sized close to the limit leaves nothing for Metaspace, thread stacks, the code cache or direct buffers, and the container limit is charged for all of them.
Where do I set JVM memory flags for Tomcat?
In `setenv.sh` in `CATALINA_BASE/bin`, using `CATALINA_OPTS`. The Tomcat documentation says not to use `JAVA_OPTS` for memory limits.
Talk to an engineer
Need a hand with Tomcat?
Senior engineers who run it in production. Get a quote or a free consultation, no sales pitch.

