What do -Xms and -Xmx mean, and how do you size Java memory in containers?
Short answer
-Xms is the starting heap size and -Xmx is the maximum heap size the JVM may use for objects. If the app needs more than -Xmx, it throws OutOfMemoryError. In containers, modern JVMs read the container memory limit, and the usual approach is -XX:MaxRAMPercentage=75 instead of a fixed -Xmx.
Why not give the heap 100% of the container memory?
The JVM uses memory outside the heap too: metaspace (class data), thread stacks, direct buffers, the JIT compiler. If heap = the full limit, total usage goes over the limit and Kubernetes kills the pod (OOMKilled, exit code 137).
Example
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"]
resources:
requests: { memory: "1Gi", cpu: "500m" }
limits: { memory: "1Gi" }
# → max heap ≈ 768 MiB, leaving ~256 MiB for non-heap memory
Two different failures
| Symptom | Meaning |
|---|---|
java.lang.OutOfMemoryError: Java heap space in logs |
Heap too small or a memory leak |
Pod status OOMKilled, exit 137, no Java error |
Whole process exceeded the container limit — heap + non-heap too big |
What to say in the interview
"Xms and Xmx set initial and max heap. In Kubernetes I use MaxRAMPercentage around 70–75% so the heap scales with the container limit and leaves room for metaspace and threads. A Java OOM error means the heap is too small or leaking; an OOMKilled pod means total memory exceeded the limit."