01
Demo 01 — Container-Aware Heap Sizing
Java 21
⏱ ~5 min
Run this demo
View source on GitHub ↗
cd demo-01-heap-sizing
chmod +x demo.sh
./demo.sh
Without UseContainerSupport, the JVM reads host RAM from /proc/meminfo and claims 25% of the node’s full memory — inside a container with a 512MB limit. Kubernetes OOMKills the pod within seconds.
What you’ll see
- JVM without
UseContainerSupport→ reads host RAM → OOMKill simulation UseContainerSupport+MaxRAMPercentage=75.0→ JVM respects the container limit- Live
jcmdoutput comparing configured heap sizes
The fix
# ❌ Hardcoded — breaks when VPA or cluster admin resizes
-Xms512m -Xmx2048m
# ✅ Container-aware — scales automatically with the limit
-XX:+UseContainerSupport
-XX:MaxRAMPercentage=75.0
-XX:InitialRAMPercentage=50.0
Why 75%?
MaxRAMPercentage=75 leaves 25% for five other JVM memory regions that most teams forget:
| Region | Typical size |
|---|---|
| Metaspace | 50–200MB |
| Platform Thread Stacks | 1MB × thread count |
| JIT Code Cache | 128–256MB |
| Direct ByteBuffers (Netty) | Varies |
| GC bookkeeping | 50–100MB |
Verify at runtime
jcmd <pid> VM.flags | grep -E "MaxHeap|RAMPercentage"