How should you size JVM heap in a container?
⚡
Quick Answer
Use -XX:MaxRAMPercentage so the JVM sizes the heap from the container's memory limit rather than the host's.
Detailed Answer
Older JVMs saw host memory and over-allocated heap, causing OOM-kills. Modern JVMs are container-aware; set -XX:MaxRAMPercentage=75 (leaving headroom for metaspace, threads, and off-heap) instead of a fixed -Xmx that ignores limit changes. Always set a container memory limit too.
Code Example
java -XX:MaxRAMPercentage=75 -jar app.jar
💡
Interview Tip
MaxRAMPercentage over fixed -Xmx in containers.
jvmcontainerssizing