Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

For a stateless JVM service with steady traffic, the team must choose between running a few large instances with 32 GB heaps or many small instances with 4 GB heaps. How do you decide?

We decide by measurement, but the measurement is guided by a few known effects. Every JVM carries a fixed overhead, so many small instances spend more memory in total and each must warm up its JIT; large heaps need a collector whose pauses do not grow with heap size and should stay just under 32 GB to keep compressed oops. Small instances limit the damage of one crash and pack better onto nodes, while large ones give local caches a higher hit rate, so we load test both shapes and compare cost per request and tail latency.

Why this question is asked

This question shows up whenever a team moves a service to Kubernetes or reviews its cloud bill. Both shapes work, and both have advocates, which is exactly why interviewers ask it: a senior answer does not pick a side by instinct but names the forces at play and then designs a test that settles it. In this answer, we will look at what a JVM costs before it handles any request, why 32 GB is a special number, how heap size affects garbage collection pauses, what operations gain from small instances, how caches change the picture, and finally how to run the comparison.

1What Every JVM Costs Before Serving a Request

A JVM is more than its heap. Each process also needs metaspace for class metadata, a code cache for JIT-compiled code, memory for the JIT compiler threads themselves, the garbage collector's own bookkeeping structures, one stack per thread, and native memory used by libraries. For a typical Spring Boot service this overhead is often 250 to 500 MB, and it is roughly the same whether the heap is 4 GB or 32 GB.

That matters at fleet scale. Suppose we need 256 GB of heap in total. Eight instances of 32 GB carry eight copies of the overhead; sixty-four instances of 4 GB carry sixty-four copies.

The same 256 GB of heap in two shapes (illustrative overhead of 400 MB per JVM)
ShapeInstancesFixed overheadTotal memory
Large: 32 GB heaps (31g in practice)88 x 0.4 GB = 3.2 GBabout 259 GB
Small: 4 GB heaps6464 x 0.4 GB = 25.6 GBabout 282 GB
The overhead is per process, so it multiplies with the instance count.

2Why 32 GB Is a Special Number

On 64-bit HotSpot, references between objects are normally stored as 4-byte compressed oops instead of full 8-byte pointers. The trick works only while the heap can be addressed with a 32-bit offset scaled by 8, which covers just under 32 GB. At -Xmx32g the JVM switches to full 8-byte references, and every reference field and array slot doubles in size.

For a reference-heavy heap, that can cost 20 to 40 percent more memory for the same data, and fewer objects fit in CPU caches. So a 33 GB heap may hold less useful data than a 31 GB heap. The rule is to stay just below the limit, for example -Xmx31g, or to go well above it, such as 48 GB or more, where the extra space outweighs the larger pointers.

Shell
java -Xmx31g -XX:+PrintFlagsFinal -version | grep UseCompressedOops
# bool UseCompressedOops = true {ergonomic}
java -Xmx32g -XX:+PrintFlagsFinal -version | grep UseCompressedOops
# bool UseCompressedOops = false {ergonomic}

3How Heap Size Affects Pauses

With G1, young pauses depend mostly on how much live data survives in the young generation, so they do not grow much with heap size. But a large heap can have a large young generation and huge remembered sets, and if anything goes wrong, a full GC of 31 GB takes many seconds. Large G1 heaps therefore need careful tuning of pause targets and marking.

Generational ZGC, the default mode of ZGC since JDK 23, does marking, relocation and reference updates concurrently, so its pauses stay well under a millisecond whether the heap is 4 GB or 64 GB. That makes it the natural choice for the large shape. One detail: ZGC does not use compressed oops at all, so with ZGC the 32 GB boundary loses its meaning, and we compare on total memory instead.

4What Small Instances Give Operations

Small instances shrink the blast radius. If one of 64 instances crashes or stalls in a long pause, we lose about 1.6 percent of capacity; if one of 8 does, we lose 12.5 percent, and the load balancer pushes that load onto the remaining seven. Small instances also pack better onto Kubernetes nodes: a 5 GB pod fits into gaps that a 34 GB pod never will, and rolling deployments can move in small steps.

The price is warmup. Each new JVM starts in the interpreter and needs thousands of calls per method before C2 compiles it to fast code. With 64 instances, every deployment and every autoscaling event means 64 warmups, and during that time latency is worse. Class data sharing and ahead-of-time caches in recent JDKs shorten startup, but the JIT warmup cost is still paid per instance.

5Caches Change the Picture

If the service keeps a local in-memory cache, such as Caffeine in front of a database, shape matters a lot. With 8 instances, each cache sees one eighth of the traffic and can hold a large share of the hot keys. With 64 instances, each cache sees one sixty-fourth of the traffic, stores mostly the same popular keys as its neighbours, and misses more often. In effect, the fleet stores 64 copies of the cache. If hit rate drives downstream load, the large shape can save a lot of database capacity, unless the cache can move to a shared store or requests can be routed by key.

6Running the Comparison

We settle it with a load test of both shapes at the same total capacity and the same traffic mix. For each shape, we record total memory and CPU reserved, the requests per second the fleet sustains at its latency target, p99 and p999 latency, GC pause and GC CPU from the logs, and cache hit rate. Then we compute cost per million requests, and we add the operational factors that a load test does not show: crash impact, deployment speed, and how well the pods pack onto our node types.

A common outcome is a middle shape, such as 8 to 16 GB heaps with generational ZGC or G1, that keeps overhead low without making each instance a large share of capacity. What matters is that the decision rests on numbers from our own service, not on a rule of thumb.

Step throughChoosing an instance shape
  1. Define total capacity and traffic mixSame requests per second for both shapes
  2. Run the large shape31 GB with G1, or larger with generational ZGC
  3. Run the small shape4 GB heaps, same total capacity
  4. Record cost and latencyMemory, CPU, p99, GC CPU, cache hit rate
  5. Add operational factorsCrash impact, warmup, node packing
  6. Pick the cheapest shape that meets the latency targetOften a middle size

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

7Follow-up questions

Why not just use 32 GB exactly, since it is a round number?

Because -Xmx32g is just over the limit where compressed oops work, so the JVM silently switches to 8-byte references and the heap holds less data than at 31 GB. If we want a large G1 heap, -Xmx31g or a value checked with PrintFlagsFinal keeps compressed oops on.

Does the choice change for a stateless service with no cache?

Yes. Without a local cache, the hit-rate argument disappears, so the decision comes down to fixed overhead, warmup and resilience. Many teams then lean to medium-sized instances, because the overhead difference is modest and the smaller blast radius is worth it.

How do containers affect the heap setting?

In a container we usually set the heap as a percentage of the memory limit with MaxRAMPercentage, leaving room for non-heap memory. With small instances, that fixed non-heap part is a bigger fraction of the limit, so the percentage often has to be lower, which further favours slightly larger instances.

8Conclusion

In this answer, we saw that the instance shape trades fixed per-JVM overhead and cache efficiency, which favour fewer large instances, against crash impact, packing and rollout flexibility, which favour many small ones. Large heaps should stay just under 32 GB to keep compressed oops, or use a collector like generational ZGC whose pauses do not grow with heap size. We settle the choice with a load test of both shapes, comparing cost per request and tail latency.

What to say in the interviewAbout 80 seconds aloud

Open with

I would not decide this by preference; I would load test both shapes, but I know which effects to look for. Every JVM has a fixed overhead for metaspace, the code cache, JIT threads, GC structures and thread stacks, often a few hundred megabytes, so sixty-four small instances spend much more memory on overhead than eight large ones.

Then, if they want more

Each small instance also has to warm up its JIT on every deployment.

On the other side, a large heap has rules: I would keep G1 heaps at 31 GB, because at 32 GB the JVM loses compressed oops and every reference doubles in size, or I would go much larger with generational ZGC, whose pauses do not grow with heap size.

Small instances limit the damage when one crashes, pack better onto nodes and roll out in finer steps. And if the service has a local cache, fewer large instances give much better hit rates.

So I would run both shapes at the same total capacity, record memory, CPU, p99 latency, GC CPU and cache hit rate, compute the cost per million requests, and then weigh in crash impact and packing. Often the answer is a middle size, but I would let the numbers decide.

About this answer

Last updated
12 November 2026
Versions it names
Java 23
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 "For a stateless JVM service with steady traffic, the team must choose between running a...": https://scrivio-interview-library.netlify.app/java/memory-layout/46a35785b5/