Sub topic 11
Sizing the JVM for Containers
This chapter assumes you can build a Spring Boot JAR, have a working Dockerfile like the one in the previous chapter, and know basic container ideas such as memory limits. You will learn how the JVM reads container limits, which flags matter, and how to avoid the most common out-of-memory kills.
Why the JVM needs sizing
A container runtime such as Docker or Kubernetes can cap a process at a fixed amount of memory and CPU. Modern JVMs (Java 10 and later, and 8u191 and later) detect those caps through cgroups, the Linux feature that enforces them. The JVM then sets its defaults from the container limit, not from the host machine.
The defaults are cautious. With no flags, the maximum heap is one quarter of the container memory. A container limited to 1 GiB gets a heap of about 256 MiB. That is safe but often wasteful for a service that owns the whole container.
Heap is not the whole process
The container limit covers everything the JVM process uses, not only the heap. The rest is called non-heap memory:
- Metaspace holds loaded class metadata.
- Thread stacks take about 1 MiB per thread by default.
- The code cache holds JIT-compiled code.
- Direct buffers and native libraries use memory outside the heap.
If you set the heap equal to the container limit, the total goes over the limit. The kernel then kills the process with an out-of-memory (OOM) kill. Kubernetes reports this as OOMKilled with exit code 137.
Setting the heap as a percentage
Use -XX:MaxRAMPercentage to give the heap a share of the container limit. This keeps the setting correct if you change the limit later.
FROM eclipse-temurin:21-jre
RUN useradd --system spring
USER spring
WORKDIR /app
COPY target/myapp-1.0.0.jar app.jar
EXPOSE 8080
ENV JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75.0"
ENTRYPOINT ["java", "-jar", "app.jar"]JAVA_TOOL_OPTIONS is an environment variable that the JVM reads at startup. Because it is an environment variable, you can override it per environment without rebuilding the image.
A value of 75 works for many services with a container of 1 GiB or more. For small containers, such as 512 MiB, the fixed non-heap overhead is a larger share, so use 50 to 60. Always check with real load, because thread counts and direct buffers differ by application.
CPU and thread pools
The JVM reads the CPU limit and sizes its internal pools from it, including garbage collector threads and the common fork-join pool. Two limits matter:
- A limit below 2 CPUs makes the JVM choose the Serial garbage collector instead of G1, because it treats the machine as a small one. Serial is fine for tiny services but pauses longer under load.
- A fractional limit such as
500mis rounded up, so the JVM sees 1 CPU.
Tomcat's thread pool does not scale with the CPU count. The default maximum is 200 threads, and each thread reserves stack memory. If you reduce server.tomcat.threads.max to match what the container can handle, you also reduce memory use.
Check what the JVM decided
Do not guess. Ask the JVM what it computed inside a container with a 1 GiB limit:
docker run --rm -m 1g -e JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75.0" \
eclipse-temurin:21-jre \
java -XX:+PrintFlagsFinal -version | grep -E "MaxHeapSize|UseG1GC|UseSerialGC"MaxHeapSize should be close to 805306368 bytes (768 MiB). With only one CPU assigned, add --cpus=1 to see the collector change to Serial.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Pod restarts with `OOMKilled` | Heap percentage too high for non-heap needs | Lower `MaxRAMPercentage` or raise the limit |
| `java.lang.OutOfMemoryError: Java heap space` | Heap too small for the workload | Raise the limit or the percentage |
| Heap stays near 256 MiB on a large container | Default 25% rule applies | Set `MaxRAMPercentage` |
| Long pauses on a small container | Serial collector chosen at under 2 CPUs | Raise the CPU limit or force `-XX:+UseG1GC` |
Exercise
A container has a 2 GiB memory limit and the JVM runs with -XX:MaxRAMPercentage=100. The service is killed with exit code 137 under load, but heap usage never reported an OutOfMemoryError. Why, and what do you change?
Show answer
The heap is allowed to grow to the full 2 GiB, but metaspace, thread stacks, the code cache and direct buffers also need memory. The total passes the container limit, so the kernel kills the process (OOM kill, exit code 137) before the JVM can throw its own error. Lower MaxRAMPercentage to about 75 (or less for a small container) so the non-heap memory has room, then verify under load.