Scrivio Interview library
LeadershipPrincipal5 min read · updated 12 November 2026

OpenJDK has ongoing work toward collectors that size the heap automatically from load and available memory. Should your organisation plan to move from explicit per-service heap settings to self-sizing JVMs over the coming years? How would you decide?

We should plan for self-sizing JVMs but not bet on them yet. Explicit heap settings give predictable memory and simple capacity planning; self-sizing heaps could save memory when load is low and adapt as services grow, which lowers cost. But container limits remain hard, so any self-sizing heap must still respect the limit and leave non-heap headroom, and the status of automatic heap sizing differs by collector and release, so check before relying on it. I would adopt it when it is a stable default in an LTS release, start with canaries and a clear rollback, and judge it by RSS, GC CPU and OOM events.

Background to this question

Fleet-wide heap settings are one of the largest levers on infrastructure cost and one of the most common causes of incidents. A change in how the JVM sizes its heap could alter both, so a senior engineer needs a view on when and how to adopt it, rather than either ignoring it or switching on the first release. The question is really about decision making under uncertainty: weighing a promising technology against operational predictability. In this answer, we will recap how heaps are sized today, see what self-sizing promises, look at the constraints containers impose, decide what evidence we need, and lay out an adoption plan.

1Where We Are Today: Explicit Heap Settings

Most container fleets today fix the heap at startup with a percentage of the pod limit, such as -XX:MaxRAMPercentage=70, or with an explicit -Xmx. The collector then works inside that maximum for the life of the process. This has real advantages. Memory use is predictable: a pod with a 4 GiB limit will use roughly its heap plus a known amount of non-heap memory. Capacity planning is simple: we multiply pod size by replica count. And failures are understandable, because an OOM means the live data outgrew a known number.

The cost is waste. A service sized for its busiest hour keeps that heap committed all night, and many teams size generously to be safe. Across a fleet, the gap between committed heap and what services need at quiet times can be large.

2What Self-Sizing Promises

A self-sizing heap lets the collector decide how big the heap should be from the current allocation rate, the live data and the available memory, growing when load rises and shrinking and returning memory when it falls. Parts of this already exist: G1 can uncommit unused memory after periodic collections, and ZGC has a soft maximum heap size it tries to stay under. OpenJDK has ongoing work and proposals for automatic heap sizing in ZGC and G1 that would go further, taking the heap size decision away from a fixed flag. The exact status differs by collector and release, so we check the current JEPs and release notes before planning around any of them.

If it works as intended, the benefits are lower memory at quiet times, less effort spent on per-service tuning, and services that adapt as they grow instead of needing a new sizing exercise.

3The Constraint That Does Not Go Away: Container Limits

However clever the JVM becomes, the container memory limit stays a hard limit enforced by the kernel. A self-sizing heap must therefore treat the limit as a ceiling, and it must leave room for non-heap memory: metaspace, thread stacks, code cache, direct buffers and native libraries. If it grows the heap into that space, the pod is OOM killed without any Java error. Also, Kubernetes schedules by requests, so memory the JVM returns when idle only saves money if the platform can use it, for example through denser packing or autoscaling of nodes.

Explicit versus self-sizing heaps
AspectExplicit percentageSelf-sizing heap
Memory usepredictable, fixed at startupvaries with load
Cost at low loadfull heap stays committedmemory returned when idle
Capacity planningpod size x replicasneeds usage data and headroom
Riskwaste, or OOM if sized too smallnew behaviour, maturity by release
Container limitrespected by our percentagemust still be respected by the JVM

4How I Would Decide

The decision depends on evidence, not on enthusiasm. I would look for four things. Maturity: is automatic sizing the default, or at least fully supported, in an LTS release we run, such as JDK 25 or a later LTS? Defaults in an LTS release get wide production use and long support. Container behaviour: does it read the cgroup limit and keep non-heap headroom by design? Savings: on our own services, how much memory does it return at quiet times, and can our platform turn that into lower cost? Stability: does GC CPU or latency get worse when the heap shrinks and has to grow back at the morning peak?

For a fleet that is mostly steady, high-traffic services, the savings may be small and explicit settings may remain the better choice. For a fleet with many services that are idle most of the day, the savings can be large. So the answer may differ by service class, and that is acceptable.

5An Adoption Plan With Canaries and Rollback

When the evidence is good, adoption follows the same pattern as any fleet-wide change. Start with a small group of non-critical services with clear daily cycles, where savings are easiest to see. Enable self-sizing on canary pods alongside pods with explicit settings, and compare.

Step throughAdopting self-sizing heaps
  1. Stable in an LTS we run?
    YesPick pilot servicesNoKeep explicit settings, revisit next LTS
  2. Canary pods with self-sizingnext to explicit-setting pods
  3. Compare for weeksRSS, GC CPU, p99 latency, OOM events
  4. Better or equal, with savings?
    YesWiden by service classNoRoll back, record why
  5. Keep the overrideexplicit percentage still available per service

5 steps in order. Play them, or step through them one at a time.

Rollback must be a configuration change, not a rebuild, so the base image keeps the explicit percentage path available. Throughout, we keep measuring RSS, GC CPU and OOM events, because those three numbers tell us whether the JVM is saving memory, spending more CPU to do it, or taking risks with the limit.

6Follow-up questions

Can we get some of the savings today?

Yes. G1 can return unused memory after periodic collections when configured to, and right-sizing pods from measured data often saves more than any heap mechanism. Both are available on current LTS releases.

What is the main risk of switching too early?

Unpredictable memory at the worst time. If the heap shrinks overnight and has to grow quickly at the morning peak, GC CPU and latency can spike, and if headroom is miscalculated the pod is OOM killed. Early adopters find these edge cases; a fleet should wait until they are fixed.

Does self-sizing remove the need for container limits?

No. Limits protect the node and the other pods, and the scheduler still needs requests. The JVM must work inside them, and the platform still sets them from measured usage.

7Conclusion

In this answer, we compared explicit heap settings, which give predictability and simple planning, with self-sizing heaps, which promise lower cost and less tuning. Container limits stay hard, so self-sizing must respect them and leave non-heap headroom, and its status differs by collector and release. The decision rests on maturity in an LTS release, container behaviour, measured savings and stability; adoption goes through canaries with an easy rollback, judged by RSS, GC CPU and OOM events.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd plan for it but not bet on it yet. Explicit settings, usually MaxRAMPercentage in the base image, give us predictable memory, simple capacity planning and failures we understand.

Then, if they want more

The cost is waste, because a heap sized for the peak stays committed all night.

Self-sizing heaps promise to fix that by growing and shrinking with load and returning memory when idle, and there is real OpenJDK work in that direction for both ZGC and G1, but the status differs by collector and release, so I'd check the current JEPs rather than assume.

Whatever happens, the container limit stays a hard kernel limit, so the JVM must still treat it as a ceiling and keep room for non-heap memory, and returned memory only saves money if the platform can actually reuse it.

My decision criteria would be: is it a stable default or fully supported in an LTS we run, does it respect cgroup limits by design, how much does it save on our own services, especially ones that are idle most of the day, and does GC CPU or latency suffer when the heap grows back at the morning peak.

If the answers are good, I'd pilot it on canaries next to explicit-setting pods, compare RSS, GC CPU, p99 and OOM events for weeks, widen it by service class, and keep the explicit percentage as a one-line rollback.

About this answer

Last updated
12 November 2026
Versions it names
Java 25
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "OpenJDK has ongoing work toward collectors that size the heap automatically from load...": https://scrivio-interview-library.netlify.app/java/jvm-tuning/ae905e72c1/