Quick revision
The short answer
We decide per service by asking whether fast startup and low memory actually change something for it, such as scale-to-zero, a command-line tool or hundreds of small instances. Native images cost longer builds, a closed-world model, reflection hints for every library and often lower peak throughput than a warmed-up JIT. JVM options such as CDS, the JDK AOT cache and Spring AOT on the JVM often give much of the startup gain with far less risk, so we pilot one service under real load and measure before choosing.
What to remember
In this answer, we saw that native images give fast startup and low memory, which matter for scale-to-zero, CLIs and large fleets of small instances, but cost longer builds, a closed-world model, hints for every library and sometimes lower peak throughput. JVM options such as CDS, the JDK AOT cache and Spring AOT on the JVM often deliver much of the startup gain with less risk. So we decide per service with a few concrete questions and confirm with a pilot that measures startup, memory, throughput and build time on real load.
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 decide per service by asking whether fast startup and low memory actually change something for it, such as scale-to-zero, a command-line tool or hundreds of small instances. Native images cost longer builds, a closed-world model, reflection hints for every library and often lower peak throughput than a warmed-up JIT. JVM options such as CDS, the JDK AOT cache and Spring AOT on the JVM often give much of the startup gain with far less risk, so we pilot one service under real load and measure before choosing.
Why this question is asked
When Spring Boot 3 made GraalVM native images a supported option, many teams asked the obvious question: if a native service starts in a tenth of a second and uses a fraction of the memory, why not compile everything that way? The honest answer is that native images trade one set of costs for another, and the trade is only worth it for some services. Interviewers ask this design question to see whether we can reason about trade-offs with numbers instead of following a trend. In this answer, we will recap what a native image is, look at what it gives and what it costs, compare it with the JVM options that also speed up startup, and finish with a decision process and a pilot plan.
1A Quick Recap of Native Images
A normal Spring service runs on the JVM. The JVM loads classes when they are first used, interprets bytecode, and its JIT compiler, the just-in-time compiler, turns hot methods into optimised machine code while the service runs. That is why a JVM service is slow for its first seconds and fast once it has warmed up.
A GraalVM native image is compiled ahead of time into a single executable. At build time, GraalVM analyses everything reachable from main, compiles it to machine code and even runs some initialisation. The result starts almost instantly and needs no JIT, but GraalVM must know at build time every class that will ever be used. That assumption is called the closed world, and it is the root of most of the costs.
2What a Native Image Gives
The benefits are real and easy to measure. A typical Spring Boot REST service that takes 2 to 4 seconds to start on the JVM may start in 50 to 200 milliseconds as a native image, and its resident memory may drop from around 250 to 400 MB to around 60 to 120 MB. These numbers are illustrative and depend on the service, so we measure our own.
These gains matter in specific situations. With scale-to-zero platforms, such as serverless containers, a request may wait for the service to start, so startup time is user latency. A command-line tool started hundreds of times a day feels instant instead of sluggish. A platform running 300 small instances of a low-traffic service saves real money when each instance needs 100 MB instead of 350 MB. For a service that runs three instances for weeks without restarting, none of this changes much.
3What a Native Image Costs
The first cost is the build. A native build commonly takes several minutes and needs several gigabytes of memory, compared with well under a minute for a jar, which slows every pipeline run and every local experiment. The second is the closed world: profiles and conditions are fixed at build time, dynamic class loading does not work, and anything using reflection needs hints. Spring generates most hints for its own features, but every third-party library must either ship its own metadata, appear in the shared GraalVM reachability metadata repository, or be covered by hints we write. A single old library that builds class names from strings can cost days.
The third cost is runtime performance under steady load. The JIT optimises using what it observes while the service runs, so a warmed JVM service often reaches higher peak throughput than the same code compiled ahead of time. Profile-guided optimisation in Oracle GraalVM narrows that gap; check the edition and licence you use. Finally, debugging and profiling tools differ from the familiar JVM ones, and the team must learn them.
4JVM Options That Close Much of the Gap
Before going native, we should try the cheaper options, because they keep the normal JVM and its tools. Class Data Sharing (CDS) stores parsed class metadata in an archive that the JVM maps at startup instead of reading and verifying classes again. Spring Boot supports creating such an archive with a training run that stops after the context refreshes. Newer JDKs extend this with the AOT cache from Project Leyden, which can also store loaded and linked classes and method profiles; Java 25 made it easier to create, so check the flags for your JDK. Spring AOT on the JVM, enabled with spring.aot.enabled=true, removes the scanning and condition work. Together these often cut startup by half or more.
| Option | Startup | Memory | Risk and cost |
|---|---|---|---|
| Plain JVM | about 3 s | about 350 MB | none, full JIT peak |
| JVM + CDS or AOT cache | about 1.5 s | about 330 MB | training run in the build |
| JVM + Spring AOT + AOT cache | about 1 s | about 320 MB | closed bean world, still JVM tools |
| GraalVM native image | about 0.1 s | about 90 MB | long builds, hints, lower peak possible |
5Deciding Per Service and Running a Pilot
With the gains and costs in front of us, the decision becomes a short set of questions for each service. Does startup time or per-instance memory change user latency or the bill? Do all dependencies have native support? Can the team accept slower builds? Is peak throughput critical? A service that answers yes to the first and has no blocking dependency is a native candidate; most others are better served by the JVM options.
- Do startup or memory change latency or cost?YesCheck dependenciesNoStay on the JVM, try CDS or AOT cache
- Do all libraries support native images?YesCheck throughput needsNoFix hints first or stay on the JVM
- Is peak throughput the main concern?YesMeasure carefully, JIT may winNoPilot the native build
- Pilot with real loadstartup, RSS, p99 latency, throughput, build time
- Gain worth the build and hints cost?YesAdopt for this serviceNoUse JVM startup options
5 steps in order. Play them, or step through them one at a time.
The pilot should use one real service and real traffic, not a hello-world. We record startup time, resident memory, p99 latency and throughput after a ten-minute steady run, build time and build memory in CI, and how many hours the hints took. With those numbers, the decision is a business conversation rather than a matter of taste, and the result often is that a few edge services go native while the rest use the JVM with CDS or the AOT cache.
6Follow-up questions
Can one codebase support both a JVM and a native build?
Yes, and it is a good practice. Spring AOT output works on both, so we can build a normal jar for most environments and a native image where it pays off. Running the native tests in CI keeps both paths working, so switching a service later does not become a project.
Why can a native image be slower under load?
Because the JIT compiler on the JVM optimises with information it gathers while the code runs, such as which branches are taken and which types actually appear at a call site. An ahead-of-time compiler must guess. For short-lived processes the native image wins easily, but for a service that runs for days at high load, the JIT's informed optimisations often give higher peak throughput. Profile-guided optimisation reduces the difference.
What about virtual threads and native images?
They are independent choices. Virtual threads change how we handle blocking IO and work on both the JVM and in native images on supported GraalVM versions. They do not affect startup, so they do not change the decision about native compilation.
7Conclusion
In this answer, we saw that native images give fast startup and low memory, which matter for scale-to-zero, CLIs and large fleets of small instances, but cost longer builds, a closed-world model, hints for every library and sometimes lower peak throughput. JVM options such as CDS, the JDK AOT cache and Spring AOT on the JVM often deliver much of the startup gain with less risk. So we decide per service with a few concrete questions and confirm with a pilot that measures startup, memory, throughput and build time on real load.
Open with
I would not compile everything native by default; I would decide per service. Native images give startup in tens or low hundreds of milliseconds and much lower memory, which matters when startup is user latency, as with scale-to-zero, for command-line tools, or for fleets of many small instances where memory is most of the bill.
Then, if they want more
The costs are real though: native builds take minutes and gigabytes of memory, the closed world fixes profiles and conditions at build time, every library needs reflection metadata or our own hints, and a warmed JIT often beats native code on peak throughput for long-running, busy services.
So for each service I ask whether startup or memory actually changes latency or cost, whether every dependency supports native images, and whether peak throughput is the main concern.
Before going native I would try the cheaper JVM options, CDS or the JDK AOT cache plus Spring AOT on the JVM, which often cut startup by half with normal JVM tooling.
For a real candidate I would pilot one service under real load and measure startup, resident memory, p99 latency, steady-state throughput, build time and the hours spent on hints. Typically a few edge services go native and the rest stay on a tuned JVM.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Spring Boot 3
- 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.
- Official documentation
- Spring Framework reference, Core technologies
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 "Teams ask whether to compile all services as GraalVM native images now that Spring...": https://scrivio-interview-library.netlify.app/spring-boot/spring-framework-evolution/a89b13167b/