Sub topic 14
Choosing native images: costs and benefits
This chapter assumes you can build a Spring Boot 3 application with Maven or Gradle, and that you have already compiled one to a native executable. It covers how to decide whether to ship that executable or stay on the JVM.
What you gain
A native executable has already done its class loading, linking and Spring AOT processing at build time. The results are usually:
- Fast startup. Startup typically drops from seconds to tens of milliseconds. This matters most for scale-to-zero platforms, serverless functions and command-line tools.
- Lower memory. There is no JIT compiler, no class metadata for unused code, and a smaller heap at idle. Resident memory is often a fraction of the JVM's.
- Immediate performance. There is no warm-up period. The first request runs at the same speed as the millionth.
- Smaller deployable. A native container image has no JDK in it, which also reduces what you must patch.
What you pay
| Area | JVM | Native image |
|---|---|---|
| Build time | Seconds | Minutes, with high memory use during the build |
| Peak throughput | The JIT optimizes hot code using runtime profiles | Often lower for long-running, compute-heavy work |
| Dynamic features | Reflection, proxies and classpath scanning work freely | They need hints or metadata, or they fail |
| Configuration | Conditions and profiles can change at startup | Many are fixed at build time |
| Debugging and tooling | Full JVM tooling, including Java agents | Limited. Some JVM tools and agents do not apply |
| Library support | Anything on the classpath | A library must work under the closed-world assumption |
Two costs deserve emphasis. First, the feedback loop is slower. Most developers iterate on the JVM and build natively in CI. Second, native-only failures appear at runtime. A missing reflection hint compiles fine and then fails when the code path runs, so you need native tests (see the testing chapter) to catch it.
Throughput also needs measuring rather than assuming. The JIT compiler can outperform ahead-of-time code on a service that runs for days. Profile-guided optimization in Oracle GraalVM narrows the gap, but it adds another build step.
A decision flow
Practical guidance
Choose native when instances start and stop often, when you pay per unit of memory or per cold start, or when you ship a short-lived CLI. Examples are autoscaled services that must respond quickly after scaling up, and many small services packed onto shared nodes.
Stay on the JVM when the service runs for a long time at steady load, when it relies on heavy reflection or dynamic class loading, or when your team depends on JVM agents and profilers. Build cost also counts. If your CI runners are small, a native build may be slow or run out of memory.
You do not have to commit all at once. Keep the JVM build as the default, add the native profile, and run the native test suite in CI. Then compare both builds on your own workload, measuring startup, resident memory and throughput under realistic load.
./mvnw -Pnative native:compile
./target/demoMeasure the time from process start to the "Started" log line, and read the process's resident memory after a warm-up request. Compare those numbers against java -jar target/demo.jar on the same machine.
Exercise
A team runs a Spring Boot API that serves steady traffic around the clock on a few large instances. Startup takes eight seconds, and nobody has complained about it. Their CI runners have little memory. Should they move to native images? Give two reasons.
Show answer
Probably not. The main native benefits are fast startup and low memory use, and neither is a problem for them, since instances rarely restart. The JIT may also give better peak throughput on a long-running service. Native builds are slow and memory-hungry, which hurts on small CI runners. They would also take on the risk of native-only failures for little gain.