Quick revision
The short answer
We reduce a Spring Boot container's memory by attacking each part of it: size the heap from measured live data rather than a large fixed value, run fewer threads by bounding pools, lowering Tomcat's maximum or using virtual threads for blocking I/O, remove unused starters so fewer classes fill metaspace, use the Serial collector for very small services and test compact object headers on JDK 25, and use CDS so read-only class data is shared between JVMs on a node. Lazy initialisation lowers startup memory too, but moves work to the first requests.
What to remember
In this answer, we saw that a JVM's memory is the heap plus metaspace, thread stacks, code cache, GC structures and native buffers, and that each technique targets one of them. We size the heap from measured live data, run fewer threads or use virtual threads for blocking I/O, remove unused starters, choose Serial GC for tiny services and test compact object headers on JDK 25, share class data with CDS, and use lazy initialisation knowing it shifts cost to the first requests.
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.
We reduce a Spring Boot container's memory by attacking each part of it: size the heap from measured live data rather than a large fixed value, run fewer threads by bounding pools, lowering Tomcat's maximum or using virtual threads for blocking I/O, remove unused starters so fewer classes fill metaspace, use the Serial collector for very small services and test compact object headers on JDK 25, and use CDS so read-only class data is shared between JVMs on a node. Lazy initialisation lowers startup memory too, but moves work to the first requests.
Why this question is asked
Memory is usually what decides how many containers fit on a node, so a service that asks for 1 GiB when it needs 400 MiB doubles its cost without anyone noticing. Interviewers ask this question to see whether we know where a JVM's memory actually goes, because every useful technique targets one specific part of it. In this answer, we will look at where the memory goes, then go through heap sizing, threads, classes, garbage collection and object headers, class data sharing, and lazy initialisation, explaining what each one saves and what it costs.
1Where a JVM's Memory Goes
The memory a container shows for a Java process, its resident set size or RSS, is more than the heap. The heap holds our objects. Around it, the JVM needs metaspace for class metadata, a code cache for JIT-compiled code, memory for the garbage collector's own structures, a stack for every thread, and native memory for things like direct byte buffers used by Netty or NIO. For a small Spring Boot service, the non-heap parts often add up to 150 MB or more.
| Area | What is in it | Main lever |
|---|---|---|
| Heap | Application objects | Size from measured live data |
| Thread stacks | One stack per thread | Fewer threads, virtual threads |
| Metaspace | Class metadata | Fewer dependencies, CDS |
| Code cache | JIT-compiled code | Usually left alone |
| GC structures | Collector bookkeeping | Simpler collector for small heaps |
| Direct buffers | Native I/O buffers | Bound buffer pools |
Each technique below targets one or two rows of this table, which is why we measure first. Native Memory Tracking, enabled with -XX:NativeMemoryTracking=summary and read with jcmd <pid> VM.native_memory summary, shows the size of each area in a running service.
2Sizing the Heap From Measured Live Data
Live data is the memory our objects still use right after a full garbage collection, the part that cannot be freed. A service might have 60 MB of live data and still run with a 1 GB heap because someone set -Xmx1g years ago. The JVM will happily use that space, because a larger heap means fewer collections, so the container's memory grows to match the setting rather than the need.
A good starting point is a heap of about three to four times the live data, then a load test to check GC time and latency. In containers, a percentage of the container limit is easier to manage than a fixed value.
Notice that we leave the other half of the container for metaspace, threads and native memory. Setting MaxRAMPercentage to 90 percent is a common mistake that ends in the container being killed for exceeding its limit, even though the heap itself never filled.
3Running Fewer Threads
Every platform thread has its own stack, reserved at 1 MB by default on most 64-bit platforms, with part of it actually committed as the thread runs. Embedded Tomcat allows up to 200 request threads by default, and each connection pool, scheduler and client library adds its own. A service with 300 threads can easily spend 50 to 100 MB on stacks and thread bookkeeping.
Virtual threads, final since Java 21, are cheap threads managed by the JVM whose stacks live on the heap and grow only as deep as needed, so blocking I/O no longer needs one expensive platform thread per request. They help most when the service spends its time waiting on databases and HTTP calls. They do not remove the need to bound connection pools, because the database still has a limit.
4Loading Fewer Classes
Every starter on the class path brings auto-configuration, and auto-configuration loads classes. A service that includes spring-boot-starter-webflux and spring-boot-starter-web, or a data starter it never uses, loads thousands of classes that fill metaspace and slow startup. Removing unused starters and dependencies is one of the cheapest wins: fewer classes means less metaspace, a smaller code cache and faster startup.
We can count loaded classes with -Xlog:class+load or jcmd <pid> VM.classloader_stats, and the condition evaluation report (start with --debug) shows which auto-configurations matched and why, which points straight at starters nobody needs.
5Choosing the Collector and Compact Object Headers
For very small services, the garbage collector itself matters. G1, the default for most server setups, keeps more bookkeeping structures than the Serial collector. The JVM already picks Serial automatically when it sees fewer than two CPUs or less than about 1.8 GB of memory, so many small containers use it without asking; choosing it explicitly with -XX:+UseSerialGC makes the choice stable. Serial pauses the application for every collection, which is fine for heaps of a few hundred megabytes but not for large heaps.
JDK 25 makes compact object headers a product feature (JEP 519), enabled with -XX:+UseCompactObjectHeaders. Every Java object has a header, and this shrinks it from 12 to 8 bytes on 64-bit platforms, which can cut heap use by 10 to 20 percent for services with many small objects. It is not the default yet, so we test it under load first.
6Sharing Class Data and the Lazy Initialisation Trade-off
CDS, Class Data Sharing, stores parsed class metadata in an archive file that the JVM maps into memory read-only. Because it is read-only and memory-mapped, several JVMs on the same node that use the same archive file share those memory pages, so ten instances do not each pay for their own copy. The pages are only shared when the processes really map the same file, for example the same image layer on the same node, and the newer AOT cache in JDK 24 and 25 builds on the same mechanism.
Finally, spring.main.lazy-initialization=true creates beans only when they are first needed. That lowers memory and time at startup, but the cost moves to the first requests, which become slower, and configuration errors that would have failed at startup now fail on a live request. It suits development and some batch jobs more than latency-sensitive services.
7Follow-up questions
Why is my container killed even though the heap is not full?
Because the container limit covers the whole process, not just the heap. If the heap takes 90 percent of the limit, metaspace, thread stacks, the code cache and direct buffers push the total over it, and the kernel kills the process. Leave room for non-heap memory and measure it with Native Memory Tracking.
Do virtual threads always reduce memory?
They reduce the memory spent on idle platform thread stacks, which is large when many requests wait on I/O. They do not help CPU-bound work, and if each virtual thread holds large objects, heap use can grow. Connection pools and other limits still need to be bounded.
Is lowering the code cache a good idea?
Rarely. The code cache holds compiled hot code; if it fills, the JIT stops compiling and the service slows down sharply. It is better to reduce what needs compiling, by removing unused dependencies, than to squeeze the cache.
8Conclusion
In this answer, we saw that a JVM's memory is the heap plus metaspace, thread stacks, code cache, GC structures and native buffers, and that each technique targets one of them. We size the heap from measured live data, run fewer threads or use virtual threads for blocking I/O, remove unused starters, choose Serial GC for tiny services and test compact object headers on JDK 25, share class data with CDS, and use lazy initialisation knowing it shifts cost to the first requests.
Open with
I would start by measuring, because the container's memory is much more than the heap: metaspace, thread stacks, code cache, GC structures and direct buffers. Native Memory Tracking with jcmd shows each part.
Then, if they want more
Then the main levers. First the heap: find the live data after a full GC in the GC log and size the heap at a few times that, usually with MaxRAMPercentage around 50 to leave room for non-heap memory, instead of an old fixed Xmx of a gigabyte.
Second threads: each platform thread has a stack, Tomcat allows 200 by default, so I would lower server.tomcat.threads.max to what the service needs, bound the pools, or turn on virtual threads for blocking I/O in Spring Boot 3.2 and later. Third classes: remove starters and dependencies the service does not use, which lowers metaspace and speeds startup.
Fourth GC: for very small heaps, Serial GC has less overhead, and on JDK 25 compact object headers can shrink the heap by ten percent or more, after testing. Fifth CDS, because the archive is mapped read-only and shared between JVMs on the same node.
And lazy initialisation can lower startup memory, but I would be careful because it moves the work and some errors to the first requests.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 21, Java 24, Spring Boot 3.2
- 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 "List practical ways to reduce the memory footprint of a Spring Boot service running in a...": https://scrivio-interview-library.netlify.app/java/startup-and-footprint/d278b137b5/