Runtime resources and JVM ergonomics
What you will learn
A container memory limit constrains more than the Java heap. HotSpot ergonomics use available resources to choose defaults, while explicit heap settings control only heap capacity. Model native overhead and safety headroom before selecting a heap limit, then verify measurements under realistic load.
- Compute a conservative memory budget with consistent units.
- Explain JVM heap and CPU ergonomics versus Kubernetes requests and limits.
- Distinguish JVM OutOfMemoryError from an external OOM kill.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples. Docker and Kubernetes recipes are source-reviewed, not executed. Companion local HTTP/JDK evidence is separate.
Compute an illustrative budget
public class Main { static long heapBudget(long limit,long nativeReserve,long headroom) { if(limit<=0 || nativeReserve<0 || headroom<0) throw new IllegalArgumentException(); long left=Math.subtractExact(Math.subtractExact(limit,nativeReserve),headroom); if(left<=0) throw new IllegalArgumentException("no heap budget"); return left; } public static void main(String[] args) { System.out.println(heapBudget(512,128,64)); }}Expected output:
320All inputs are MiB. Subtract reserve and safety headroom, and reject infeasible budgets.
Requests, limits and ergonomics
Kubernetes requests influence scheduling; CPU limits can throttle execution and memory limits can lead to OOM kills. On supported Linux cgroup configurations HotSpot container awareness informs resource ergonomics. -XX:MaxRAMPercentage influences maximum heap sizing relative to the JVM-selected memory basis; -Xmx explicitly sets a maximum heap. -XX:ActiveProcessorCount overrides the CPU count used for JVM ergonomics, not the OS quota. Inspect flags and actual container metrics; defaults vary by JDK, environment and collector.
Account for non-heap memory
Include metaspace, code cache, thread stacks, direct buffers, GC structures, native libraries and other process memory plus margin. A hypothetical 512 MiB limit minus 128 MiB native reserve and 64 MiB margin leaves 320 MiB for heap. These are illustrative assumptions, not a tuning prescription. An OOM kill may occur without a Java exception or a shutdown hook. Use exit reason, cgroup events, JVM diagnostics and workload evidence.
Inspect actual JVM heap without fixed assumptions
public class Main {
public static void main(String[] args) { long max=Runtime.getRuntime().maxMemory(); System.out.println(max>0); System.out.println(Runtime.getRuntime().availableProcessors()>=1); }}Expected output:
truetrueRuntime values are JVM observations, not measured total RSS or scheduler allocation.
Compare requested and limited CPU
public class Main { record Cpu(int requestMilli,int limitMilli) { int burstMilli(){return limitMilli-requestMilli;} } public static void main(String[] args) { System.out.println(new Cpu(250,1000).burstMilli()); }}Expected output:
750The difference is a fictional scheduling headroom model, not guaranteed CPU availability.
Common mistakes
Do not set -Xmx equal to the whole container limit, interpret CPU requests as dedicated cores, or diagnose every exit 137 as OOM without evidence. Do not assume a heap dump is available after SIGKILL.
Practice
For a 768 MiB memory limit, reserve 200 MiB and 80 MiB of headroom. Print 488. Reject an exhausted budget.
Expected output:
488Compare with the manual review criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Runtime resources and JVM ergonomics — knowledge check
Container deployment and operations reasoning.
What does Xmx constrain?
What does a CPU request influence?
What does ActiveProcessorCount change?
What is possible with external OOM kill?
What is the modeled heap budget for 512 minus 128 minus 64 MiB?