Sub topic 14
Native images with GraalVM: trade-offs
This chapter assumes you can build a Spring Boot JAR with Maven or Gradle and have built a container image from it, as covered in earlier chapters. It also assumes you know what a JVM does at run time. Native images are an alternative packaging path, and for most applications they are not the right default.
What a native image is
A GraalVM native image is an executable produced by ahead-of-time (AOT) compilation. Instead of shipping bytecode that a JVM interprets and compiles while the app runs, the build compiles your application, its libraries, and the parts of the JDK it uses into one machine-code binary for a specific operating system and CPU architecture.
Spring Boot supports this through its AOT processing. At build time it inspects your beans and configuration and generates the code and metadata that would normally be worked out by reflection at start-up.
What you gain
- Fast start-up. Many applications start in tens of milliseconds instead of seconds, because there is no class loading, no JIT warm-up, and less bean processing at start-up.
- Lower memory use. The process does not carry a JIT compiler or loaded bytecode, so resident memory is often noticeably smaller.
- A small, self-contained artifact. The binary can run on a minimal base image with no JRE installed.
These gains matter most for scale-to-zero workloads, short-lived functions, and command-line tools, where start-up time is a large share of total run time.
What it costs
| Area | JVM JAR | Native image |
|---|---|---|
| Build time | Seconds to a minute | Several minutes, with high memory use |
| Peak throughput | JIT optimizes hot code over time | Often lower for long-running, CPU-heavy work |
| Dynamic features | Reflection, proxies, and resources work freely | Must be known at build time |
| Portability | Same JAR on any JVM | One binary per OS and architecture |
| Debugging and tooling | Full JVM tooling | Fewer tools, different profiling |
The central limit is the closed-world assumption. The compiler must see every class that could be used, so reflection, dynamic proxies, resource loading, and serialization need configuration hints. Spring and many popular libraries supply those hints. A library that does not will fail at run time, not at build time, which is the most common source of surprises.
Building one
The Spring Boot plugins build a native image inside a container with Cloud Native Buildpacks, so you do not need GraalVM installed locally. This reuses the approach from the buildpacks chapter.
# Maven
./mvnw -Pnative spring-boot:build-image
# Gradle
./gradlew bootBuildImageWith Maven, the native profile comes from the spring-boot-starter-parent. With Gradle, applying the org.graalvm.buildtools.native plugin makes bootBuildImage produce a native image. To build a plain executable on your own machine, install a GraalVM JDK and run ./mvnw -Pnative native:compile or ./gradlew nativeCompile.
Test the native build
Passing JVM tests does not prove the native binary works. Missing hints appear only when the compiled program runs. Run the application tests in native mode as well, with ./mvnw -PnativeTest test or ./gradlew nativeTest, and start the built image against a realistic configuration before you release it.
Deciding
A JVM deployment is the sensible default. Choose native when a measured start-up or memory problem justifies slower builds and extra testing. Measure first, because modern JVMs with tuned container settings are often fast enough.
Exercise
Your team's Spring Boot service runs continuously on three Kubernetes pods, is CPU-heavy, and starts in 6 seconds, which is acceptable. A colleague proposes switching to a native image. Give two reasons to hesitate.
Show answer
Long-running, CPU-heavy work often reaches higher peak throughput on a JVM, because the JIT optimizes hot code, while a native image may be slower. Start-up time is not a problem here, so the main benefit is unused, while the costs remain: longer builds, per-platform binaries, extra native testing, and run-time failures if a library lacks native hints.