Quick revision
The short answer
A JVM's fixed overhead, metaspace, code cache, JIT, GC structures and threads, often exceeds 100 MB, so hundreds of services with tiny heaps waste most of their 512 MiB. The options, from lowest to highest risk, are right-sizing heaps and threads, sharing read-only class data between JVMs on a node with CDS, consolidating services that belong together into a modular monolith, and moving small, stable services to native image. We choose per service class, based on measurements and agreements with the owning teams.
What to remember
In this answer, we saw that every JVM carries 100 MB or more of fixed overhead, which is why hundreds of services with small heaps waste most of their memory. We measured with Native Memory Tracking, then ordered the options by risk: right-sizing heaps and threads first, CDS to share class data between JVMs on a node, consolidation into a modular monolith where services belong together, and native image for small, stable services, choosing per service class with owners and measured targets.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A JVM's fixed overhead, metaspace, code cache, JIT, GC structures and threads, often exceeds 100 MB, so hundreds of services with tiny heaps waste most of their 512 MiB. The options, from lowest to highest risk, are right-sizing heaps and threads, sharing read-only class data between JVMs on a node with CDS, consolidating services that belong together into a modular monolith, and moving small, stable services to native image. We choose per service class, based on measurements and agreements with the owning teams.
Why this question is asked
When a company runs hundreds of small services, the memory bill is dominated by something no individual team sees: the cost of running hundreds of separate JVMs. Each team's 512 MiB looks modest, but 400 services with two or three instances each reserve more than 400 GiB for applications that together hold perhaps 30 GiB of real data. Interviewers ask this design question to see whether we can reason about a fleet rather than a single service, and pick options in order of risk. In this answer, we will see where the overhead comes from, measure it, and go through four options with their costs, before choosing per class of service.
1Why a 100 MB Heap Needs Much More Than 100 MB
The heap is where the application's objects live, but the JVM needs memory around it before the application does anything. Metaspace holds the metadata for every loaded class, and a Spring Boot service loads 10,000 to 20,000 classes. The code cache holds machine code produced by the JIT compilers, which themselves use memory while compiling. The garbage collector keeps bookkeeping structures sized to the heap and the number of CPUs. Every platform thread has a stack. Together these commonly add 100 to 200 MB to a Spring Boot service, regardless of how small its heap is.
So when a service with 80 MB of live data runs in 512 MiB, the waste is not in the heap alone. Some of it is the fixed overhead, which we can shrink but not remove, and some is slack, memory requested and never used.
2Measuring Before Choosing
We start with numbers per service, because the right option depends on where the memory goes. Native Memory Tracking reports each JVM area.
Notice that committed memory, what the process really uses, is about 310 MB here, while the container asks for 512 MiB, and the heap is less than half of the total. Collecting this for every service, with each service's container request and actual peak usage, tells us how much each option could save.
3Option 1: Right-Size Heaps and Threads
The lowest-risk option is to stop asking for memory we do not use. We size each heap from its measured live data, set the container request close to measured peak usage plus a margin, lower Tomcat's maximum threads from the default of 200 to what the service needs, bound connection pools, or use virtual threads for blocking I/O. On a small collector such as Serial, GC structures also shrink.
In the example above, a 512 MiB request could become 352 MiB with no change in code, about a 30 percent saving. Across 400 services with two instances each, that alone frees over 120 GiB. It is a configuration change, reversible in minutes, so it comes first.
4Option 2: Share Class Data Between JVMs With CDS
CDS, Class Data Sharing, stores parsed class metadata in an archive that the JVM maps into memory read-only. When several JVMs on the same node map the same archive file, the operating system shares those pages, so the class metadata is paid for once rather than per process. The AOT cache in JDK 24 and 25 builds on the same archive mechanism and also speeds startup.
There is a catch: pages are only shared if the processes map the very same file. In containers, that means the archive must come from the same image layer on the same node, such as a shared base image with a common archive of the JDK and Spring classes. Application-specific archives in separate images give startup gains but little sharing. So this option works best combined with a standard base image across the fleet.
5Option 3: Consolidate Into a Modular Monolith
Some small services exist because of an old split that no longer matches how teams work: three services owned by the same team, deployed together, always changing together. Running them as modules inside one application, a modular monolith, removes two JVMs' worth of fixed overhead and often some network calls between them.
The cost is isolation. One module's memory leak or bad deployment affects the others, they share a release cycle, and the boundaries between modules must be enforced in code, for example with Spring Modulith or ArchUnit rules, instead of by the network. This is a design and ownership decision, not a memory tweak, so it only makes sense where the services already belong together.
6Option 4: Native Image for Small, Stable Services
GraalVM native image compiles a service ahead of time into an executable with no JIT and much less metadata, so a small service can drop from 300 MB to well under 100 MB, and it starts in milliseconds. The cost is longer builds, reachability metadata for reflection, native tests and different tooling, which is acceptable for small services with simple dependencies that rarely change, and painful for busy, complex ones.
| Service class | Main option | Why |
|---|---|---|
| Every service | Right-size heap, threads, request | Low risk, large total saving |
| Many instances per node, same stack | CDS from a shared base layer | Class data shared, faster startup |
| Same team, deployed together | Merge into a modular monolith | Fewer JVMs, isolation not needed |
| Small, stable, simple dependencies | Native image | Footprint and startup dominate |
| Large or complex, busy | Stay on tuned JVM | Overhead is small next to the heap |
7Follow-up questions
How do you get teams to agree to these changes?
With their own numbers. A per-service report showing request, actual peak and the saving from right-sizing makes the case. For consolidation and native image, the platform team offers templates and support, and the decision stays with the owning team, written down with the expected saving and a review date.
Is there a risk in lowering container memory requests?
Yes, if we cut below real peaks. We size from peak usage over a representative period, including deployments and traffic spikes, keep a margin, and watch for out-of-memory kills and GC time after the change. Because it is configuration, it can be rolled back quickly.
Why not just put all services on native image?
Because most of the saving comes from right-sizing at much lower cost, and native image adds build time, metadata and testing for every service. It is worth it where footprint and startup dominate, not as a fleet-wide default.
8Conclusion
In this answer, we saw that every JVM carries 100 MB or more of fixed overhead, which is why hundreds of services with small heaps waste most of their memory. We measured with Native Memory Tracking, then ordered the options by risk: right-sizing heaps and threads first, CDS to share class data between JVMs on a node, consolidation into a modular monolith where services belong together, and native image for small, stable services, choosing per service class with owners and measured targets.
Open with
I would start by explaining where the memory goes. Each JVM has fixed overhead beyond the heap: metaspace for ten thousand or more classes, the code cache and JIT, GC structures and thread stacks, often 100 to 200 MB, so a service with an 80 MB heap really needs around 300 and is asking for 512.
Then, if they want more
Then I would measure every service with Native Memory Tracking and compare request against actual peak. The first option is right-sizing heaps, container requests and threads, because it is just configuration, low risk, and across hundreds of services it frees a lot, maybe a third of the memory.
The second is CDS, because a read-only archive mapped by several JVMs on the same node is shared by the operating system; that works best with a common base image layer, and the AOT cache gives startup gains on top.
The third is consolidating services that belong to the same team and change together into a modular monolith, which removes whole JVMs but trades away isolation, so it is an ownership decision. The fourth is native image for small, stable services with simple dependencies, where footprint and startup matter most.
I would decide per class of service, with measurements and an agreement with each owning team rather than a mandate.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24
- 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 "Your company runs hundreds of small JVM services, each with at least 512 MiB of memory...": https://scrivio-interview-library.netlify.app/java/startup-and-footprint/7b1c7f5c4b/