Skip to the page

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.

The 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=70 to 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, then jcmd <pid> VM.native_memory summary and summary.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

  1. Pods are OOMKilled but the heap is only 60 percent used. What is using the memory?
  2. A Java 21 service got slower after moving to 1 CPU and 1 GiB pods. Why?
  3. Why is the first minute after each deploy slow?
  4. What settings do you check for a JVM in a container?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview