GraalVM Native Image and Spring AOT: When It Pays, and What It Costs
One ticket desk, two images. The JVM image starts in 3.098 s with 197 MiB; the native image starts in 0.092 s with 35 MiB, and takes 3 min 53 s to build instead of 48 s. How AOT moves the work, the closed world, and a rule for choosing.
What you will learn
- Explain which start-up work moves from run time to build time with AOT
- Build a native image with mvn -Pnative spring-boot:build-image
- Compare start-up, memory, image size and build time on the same app
- Know the closed-world rule and why Class.forName needs a hint
Before you start
- Containerising Spring Boot: a Layered Dockerfile vs Buildpacks
- Spring Boot basics
We take one small ticket desk app (Spring Boot 4.1.2, Java 25) and build it two ways: a normal JVM image and a GraalVM native image. Then we compare how fast each starts, how much memory it needs and how long it takes to build, and end with a simple rule for when native is worth it.
Start it on the JVM
The main class is a plain Spring Boot app:
@SpringBootApplication
public class TicketDeskApplication {
public static void main(String[] args) {
SpringApplication.run(TicketDeskApplication.class, args);
}
}
Run the JVM image in a container and the log ends with:
Started TicketDeskApplication in 3.098 seconds
It used 197 MiB of memory. Every time this app starts, Spring scans the classes, finds the beans and builds the objects, and the JIT compiler then compiles the busy code while the app runs. That work happens on every single start.
Where the work happens
On the JVM, the build only packages the jar and everything else happens at run time. With a native image it's the other way round. At build time, the native-image tool analyses the whole app and compiles it to machine code, and Spring AOT prepares the bean definitions ahead of time. At run time there's very little left to do.
Build the native image
mvn -Pnative spring-boot:build-image
This uses the native profile with the same Buildpacks goal as before. It builds inside a container (with the BellSoft Liberica native image kit), so you don't need GraalVM installed on your machine. The native-image tool runs eight steps. The first big one is the analysis: starting from main, it finds every type the app can reach, 18,278 types even for this tiny app. The other big one is compiling those methods to machine code. On this run the eight steps took 9.0, 33.2, 7.9, 3.1, 2.3, 39.1, 21.9 and 9.8 seconds: 2 min 10 s for native-image, 3 min 53 s for the whole build (including first-time downloads).
Same app, two images
| JVM image | Native image | |
|---|---|---|
| Start-up | 3.098 s | 0.092 s |
| Memory | 197 MiB | 35 MiB |
| Image size | 670 MB | 239 MB |
| Build time | 48 s | 3 min 53 s |
Native wins on start-up, memory and size: no JIT and no class loading at run time, and the image carries only the code the app really uses. It pays for that in build time. Your numbers will differ; the pattern won't.
The catch: a closed world
@Service
public class TicketService {
// Built once at start-up: the same three tickets on every run
private final List<Ticket> tickets = List.of(
new Ticket(1, "Reset Emma's password", "Tom", "OPEN"),
new Ticket(2, "Printer jams on floor 2", "Priya", "IN_PROGRESS"),
new Ticket(3, "New laptop for Daniel", "Tom", "DONE"));
public List<Ticket> all() {
return tickets;
}
// BREAK IT: finding a class by its name at run time is invisible to native-image
// Class.forName("org.studyeasy.s03l05.PriorityRule");
}
The list of tickets is plain code, reached from main through Spring, so Ticket and TicketService end up in the image. Now look at the commented line. Class.forName finds a class by a name in a string, at run time. The build can't see a string turning into a class, so that class is left out of the native image. On the JVM it works; in a native image it would fail when that line runs.
This is the closed world: the build starts from main, follows every call, keeps everything it can reach, and drops the rest. Nothing can be added later at run time. The fix is a hint that tells the build "keep this class". Spring adds many hints for you (for beans, configuration, and so on); for your own reflection or by-name lookups, you add them yourself.
When native image pays
| Native pays when | The JVM stays better when |
|---|---|
| you run many short-lived copies | one long-running service with steady traffic (the JIT keeps tuning) |
| the app scales to zero and must start fast | your libraries load classes by name |
| memory per copy is what you pay for | you build many times a day |
| command-line tools and functions |
The rule in one line: is start-up or memory what you pay for? Then native. Otherwise, stay on the JVM. And measure first. The saving is real, and so are the longer build and the closed world.
Common mistakes
- Going native "because it's faster". It starts faster. For one long-running service, start-up hardly matters, and the JIT keeps tuning the busy code while it runs.
- Testing only on the JVM. A by-name lookup passes every JVM test and fails in the native image. Test the native build too.
- Native builds on every commit. Four minutes per build adds up. Build native in the release pipeline.
Try it yourself
Time your own app's JVM start-up from the "Started ... in" log line, and check its memory with docker stats. Which row of the "when native pays" table does your app fall into?
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
