JVM, memory and Java versions · senior
The JVM in containers and at startup
In a small container the JVM may pick Serial GC and a small heap. Native memory can pass the container limit while the heap looks fine. New pods are slow until the JIT warms up.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- With fewer than 2 CPUs or less than 1792 MB the JVM does not treat the machine as server-class and picks Serial GC.
- Default maximum heap is 25 percent of container memory (
MaxRAMPercentage). Set-XX:MaxRAMPercentage=70to 75 and choose the collector explicitly, for example-XX:+UseG1GC. - A CPU limit is a quota per period. GC, JIT and application threads share it, and then every thread waits. The CPU count also sizes GC threads, JIT threads, the common ForkJoinPool and virtual-thread scheduler.
- Old JDK 8 and 11 updates may not understand cgroup v2 limits at all. Check the version you run.
- OOMKilled while the heap graph looks fine: committed heap, Metaspace, thread stacks, code cache, direct buffers and malloc fragmentation all count toward the limit.
- Measure:
-XX:NativeMemoryTracking=summary, thenjcmd <pid> VM.native_memory summaryandsummary.diff. Cap direct memory with-XX:MaxDirectMemorySize. Alert on container memory against the limit. - Slow first minute: classes load lazily and code starts in the interpreter. Hot methods are compiled by C1 then C2 using CPU at the same time as traffic. Readiness says up, not warm.
- Fixes without a new JDK: a warm-up step before readiness, slow-start in the load balancer, smaller rollout batches. Java 24 and 25 add an AOT cache (Project Leyden) for faster start. CRaC and GraalVM native image are other routes with costs. Prove every change with a load test in the same pod shape.
The red pen
Common traps: mistake → fix
Mistake: The heap graph shows total JVM memory
Fix: it shows heap only; native memory is extra.
Mistake: A pod with 1 CPU behaves like a VM with 4 cores
Fix: it can get Serial GC, throttling and a small heap.
Mistake: Ready means warm
Fix: readiness only says the process answers.
Easy to hard
Questions interviewers ask, easy to hard
- Pods are OOMKilled but the heap is only 60 percent used. What is using the memory?
- A Java 21 service got slower after moving to 1 CPU and 1 GiB pods. Why?
- Why is the first minute after each deploy slow?
- What settings do you check for a JVM in a container?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.