Java 25 Starts Your Spring Boot App 2x Faster. Here’s How

Java 25 can bring a typical Spring Boot service up in about half the time, with one extra step in the build. I measured five popular ways to start Java faster on the same application, starting each one at least 10 times and testing it under load, to see how much each one helps and where it matters most.

Janardhan Chejarla
Share

Where faster startup helps

So where does a faster start really make a difference? Mostly in places where Java starts often, or where someone is waiting for it to come up. Here are the common ones:

  • Kubernetes recovery: when a node fails, its pods are back up on other nodes sooner.
  • Scaling under traffic: when traffic grows and more JVMs or pods are started on the fly to handle it, each new one is ready to take load sooner.
  • Command-line tools and short jobs: a CommandLineRunner or batch job spends its time on its work, not on starting.
  • Desktop applications: a Java desktop app, such as a Swing or JavaFX tool or an IDE, opens its window sooner after you start it.
  • Serverless and scale-to-zero: shorter cold starts (the first start after the app was stopped while idle), so the first request waits less.
  • Tests and local development: every restart of the application context is faster.

The short answer

Started the usual way, with java -jar app.jar, the test service took 7.5 s to be ready on 2 CPUs. With the JDK AOT cache, available from Java 24 onwards, it took 4.0 s, about half the time, for one training run in the build. Spring AOT speeds up a different part of startup, the way Spring wires its beans, so the two add up. Together they brought it to 3.1 s, about 60% faster than the usual start.

Bar chart of time to ready for seven setups at 1 and 2 CPUs. Plain java takes 14.3 s at 1 CPU and 7.5 s at 2 CPUs; the JDK AOT cache 7.2 s and 4.0 s; Spring AOT plus the AOT cache 6.1 s and 3.1 s; lazy initialization does not help.
Time from launch to the readiness endpoint returning UP. Spring Boot 4.1.2 on JDK 25.0.5, median of 10 runs per bar.
OptionReady, 1 CPUReady, 2 CPUsExtra build step
Plain java (no startup option)14.3 s7.5 sNone
Lazy initialization14.5 s7.6 sNone (one property)
Spring AOT11.8 s6.1 sAOT processing at build time (9 s here)
CDS archive9.0 s4.9 sOne training run (10 s here)
-XX:TieredStopAtLevel=17.4 s5.5 sNone (one flag), but half the speed or less once warm
JDK AOT cache7.2 s4.0 sOne training run (17 s here)
Spring AOT + JDK AOT cache6.1 s3.1 sBoth of the above

What to use. On Java 25, start with the JDK AOT cache for the biggest gain from one build step, and add Spring AOT for more. On Java 21, CDS is the best option. It is about a third faster, with the same kind of training run. The flag -XX:TieredStopAtLevel=1 is only for tests and small tools that finish within seconds and cannot add a build step. With one flag, it starts much faster than plain java. Do not use it for a service or a long-running job. Once warm, the service here handled about half as many requests per second at 1 CPU, and about a third as many at 2 CPUs, for as long as it ran.

What AOT means here

AOT stands for ahead of time, meaning work that is done before the application runs, instead of while it starts. Two of the options below have AOT in their name, and they are different things:

  • Spring AOT runs at build time. It works out how to create every bean and writes that down as plain Java code, so Spring does not have to figure it out again at each start.
  • The JDK AOT cache comes from a training run. The JVM starts the application once, records the classes it loaded and linked and the methods that got hot (the methods that ran most often), and saves them in a file that later starts read.

Neither one compiles your application to a native executable. That is what GraalVM native image does, covered at the end. Both still run on a normal JVM with its JIT compiler.

What was measured

The test application is a small shop service built with Spring Boot 4.1.2, Spring MVC, Jackson, Bean Validation, Actuator and Spring Data JPA with Hibernate on an in-memory H2 database. It has four entities, four repositories and five endpoints. It is started from the extracted layout (an app.jar plus a lib folder of JARs), which Spring Boot recommends for CDS and the AOT cache because both need plain JAR files on the class path.

For each option I measured three things, with the JVM pinned to 1 CPU and to 2 CPUs:

  • Time to ready: from launching java to /actuator/health/readiness answering 200, which is the moment Kubernetes would start sending traffic. Ten runs each.
  • Warm-up at a steady rate: 300 requests per second from the moment the app is ready, with latency recorded per 5-second window. Five runs each.
  • Full load from the first request: 16 connections sending requests back to back for 120 seconds, counting completed requests. Five runs each.

The machine is small on purpose (2 vCPUs, 7 GB RAM), because that is where startup and warm-up hurt most, as in a container with a CPU limit of 1 or 2. Every number in this post is a median from those runs, and the method section has the details.

1. CDS: load classes from an archive

Class Data Sharing stores classes that the JVM has already loaded, parsed and verified in an archive file. At startup the JVM maps the file into memory instead of reading and checking every class from the JARs again. Spring Boot documents a training run that starts the application, stops it as soon as the context is refreshed, and writes the archive on exit:

Shell
# training run: start, exit after the context refresh, write the archive
java -XX:ArchiveClassesAtExit=app.jsa -Dspring.context.exit=onRefresh -jar app.jar

# every later start
java -XX:SharedArchiveFile=app.jsa -jar app.jar

With the archive, time to ready dropped from 14.3 s to 9.0 s at 1 CPU (37%) and from 7.5 s to 4.9 s at 2 CPUs (34%). Throughput in the first two minutes under load stayed the same as plain java. The archive itself is 95 MB.

The archive only works with the same JDK build and the same class path, so create it in the same image the app runs in, usually as a step in the Dockerfile. If anything differs, the JVM ignores the archive and starts as slowly as before.

2. The JDK AOT cache: CDS and more

The AOT cache from Project Leyden builds on CDS, and Spring Boot recommends it over CDS on Java 25 and later. On top of the parsed classes, it stores classes already loaded and linked (JEP 483), and since JDK 25 the method profiles collected during the training run (JEP 515), so the JIT compiler knows from the first second which methods are hot. JDK 25 also made training a single step (JEP 514):

Shell
# training run: the cache is written when the JVM exits
java -XX:AOTCacheOutput=app.aot -Dspring.context.exit=onRefresh -jar app.jar

# every later start
java -XX:AOTCache=app.aot -jar app.jar

With -Xlog:aot the JVM confirms that it uses the cache:

Text
[0.005s][info][aot] Opened AOT cache app-refresh.aot.

This was the best single option for startup. Time to ready was 7.2 s at 1 CPU and 4.0 s at 2 CPUs, about half of plain java. Spring's own log line shows the same (2 CPUs):

Text
plain java:     Started ShopApplication in 6.294 seconds (process running for 6.841)
with AOT cache: Started ShopApplication in 3.123 seconds (process running for 3.626)

The cache is 120 MB. A cache trained under real traffic for 60 seconds instead of stopping at the context refresh was 141 MB and started equally fast.

Like the CDS archive, the cache only works with the same JDK build and class path. If your service takes heavy traffic the moment it starts, read the heavy-load section before you switch the cache on.

3. Spring AOT: do the bean wiring at build time

At startup, Spring scans the class path, evaluates conditions and works out how to create each bean, mostly with reflection (code that looks inside classes while the program runs). Spring AOT does that work at build time and generates plain Java code that registers the beans directly. The Spring Boot Maven and Gradle plugins run it (process-aot and processAot). The application then has to be started with a property that tells it to use the generated code:

Shell
java -Dspring.aot.enabled=true -jar app.jar

On its own, Spring AOT took time to ready from 14.3 s to 11.8 s at 1 CPU (17%) and from 7.5 s to 6.1 s at 2 CPUs (19%). That is less than CDS, but it adds to the AOT cache. Together they reached 6.1 s and 3.1 s, the fastest setup measured, and the lowest memory at ready of the full-speed setups (265 MB against 299 MB for plain java at 2 CPUs).

The price is that the bean definitions are fixed at build time. Conditions such as @ConditionalOnProperty and profiles are evaluated once, during the build, so a property that would switch a bean on or off at runtime no longer does.

4. Lazy initialization: no gain for a web service

Setting spring.main.lazy-initialization=true tells Spring to create beans only when something first uses them, instead of all of them at startup. It is one of the most common tips for a faster start:

Shell
java -Dspring.main.lazy-initialization=true -jar app.jar

It made no measurable difference here, with 14.5 s against 14.3 s at 1 CPU and 7.6 s against 7.5 s at 2 CPUs. Most of the startup time in a typical service goes to beans that are needed before the app can report ready anyway, such as the web server, the data source, Hibernate and the repositories. Lazy initialization only defers the rest, and pays for it on the first requests.

It also moves failures later. A bean that fails to start now fails on the first request that needs it, after the readiness probe has already passed.

5. TieredStopAtLevel=1: fast to start, slow forever

HotSpot compiles hot methods in tiers, first with the quick C1 compiler and then, for the hottest methods, with the optimizing C2 compiler. -XX:TieredStopAtLevel=1 stops at C1, so the JVM spends no CPU on C2 compilations at startup:

Shell
java -XX:TieredStopAtLevel=1 -jar app.jar

It does start fast, with 7.4 s at 1 CPU, close to the AOT cache, and 5.5 s at 2 CPUs. It also had the best latency in the first minute at a steady rate. But once warm, the service handled 991 requests per second at 1 CPU against 2,073 for plain java, and 1,735 against 4,868 at 2 CPUs. That is 52% and 64% less, for as long as the process runs.

That loss never goes away, so never use this flag for a service or any long-running process. It suits processes that finish within seconds, such as tests, local development restarts, small command-line tools, and a CommandLineRunner that does one short task and exits. It is a bad fit for a batch job that keeps the CPU busy for minutes, because the slower code soon costs more than the 2 seconds saved at start.

Beyond the JVM: native images and CRaC

Two more approaches make startup almost instant, but they change much more than a flag. Here is what each one does and how to try it with Spring Boot.

GraalVM native image

A native image is the application compiled ahead of time into a native executable. Startup drops to a fraction of a second and memory use is lower. The trade-offs are long builds, the configuration that reflection and dynamic proxies need (Spring AOT generates most of it), and a peak throughput that is usually lower than the JIT's unless profile-guided optimization is used.

To build one, install a GraalVM distribution for JDK 25, such as Liberica Native Image Kit 25, and use the native profile of the Spring Boot Maven plugin, as the Spring Boot guide shows:

Shell
# build the native executable into target/
mvn -Pnative native:compile
# start it (the file is named after the project)
target/myproject

With Gradle, the task is gradle nativeCompile, and the executable goes to build/native/nativeCompile. To get a container image instead, without installing GraalVM, run mvn -Pnative spring-boot:build-image.

CRaC

CRaC (Coordinated Restore at Checkpoint) takes a snapshot of a running JVM and restores it later, so the new process starts where the old one stopped, already warm if it had served traffic. It works on Linux only and needs a JDK build with CRaC, such as Azul Zulu or BellSoft Liberica with CRaC, plus the org.crac:crac library (1.4.0 or later) on the class path. Spring closes and reopens its sockets, files and thread pools around the checkpoint, but other connections your code opens may need the same care.

The simplest way, described in the Spring Framework documentation, takes the snapshot as soon as the context is refreshed:

Shell
# take the snapshot once the context is refreshed
java -Dspring.context.checkpoint=onRefresh -XX:CRaCCheckpointTo=./checkpoint -jar app.jar
# every later start restores from it
java -XX:CRaCRestoreFrom=./checkpoint

To snapshot a warmed-up application instead, start it with -XX:CRaCCheckpointTo=./checkpoint only, send it some traffic, then run jcmd app.jar JDK.checkpoint. The snapshot files can hold secrets from memory, such as passwords and tokens, so keep them as safe as the secrets themselves.

One thing to check under heavy load

Startup time is only half of what a new instance needs. When it reports ready, its code is still mostly interpreted, and it serves requests slowly until the JIT compiler has compiled the hot paths. A new pod that joins a busy service, for example after a scale-up, takes its share of traffic right away.

So I measured the first two minutes of each setup under full load, starting the moment the app reported ready. CDS behaved like plain java. The AOT cache did not:

Line charts of requests completed per second during the first 120 seconds under full load, at 1 and 2 CPUs. The AOT cache with its stored profiles stays below plain java for the whole two minutes; the same cache with profiles turned off tracks or beats plain java.
Requests completed per second under full load, starting when the app reported ready. Median of 5 runs per 5-second window.
Bar charts of total requests in the first 120 seconds. At 2 CPUs: plain java 339k, CDS 340k, AOT cache 269k (20% fewer), the same cache with profiles off 355k (5% more).
Total requests in the first 120 seconds under full load. Median of 5 runs, with percentages against plain java.

With the cache trained to startup only (stopped at the context refresh), the service completed 20% fewer requests than plain java in the first two minutes, at both 1 and 2 CPUs. At 2 CPUs the five runs did not overlap. The best run with the cache was still below the worst run without it.

The cause is the method profiles. JDK 25 replays the profiles stored in the cache by default, and a diagnostic flag turns that off while keeping everything else the cache does. With the same cache file and replay off, startup stayed as fast and the first two minutes were 5% better than plain java at 2 CPUs:

Shell
java -XX:AOTCache=app.aot \
     -XX:+UnlockDiagnosticVMOptions -XX:-AOTReplayTraining \
     -jar app.jar

The JIT compiler's own statistics (-XX:+CITime) show what goes wrong. After 60 seconds of full load, the total compile time was about the same with and without the profiles, but it went to the wrong place:

Grouped bar chart of methods compiled at tiers 2, 3 and 4 after 60 seconds of full load. With the stored profiles, far more methods stay at tier 2 and fewer reach tier 3 and C2 than with plain java or with profiles off.
Methods compiled per tier after 60 s of full load at 2 CPUs, from -XX:+CITime.

With the profiles, 6,718 methods were compiled at tier 2, a C1 level that collects little profiling data, against 3,869 for plain java. Fewer methods moved on to tier 3, where C1 collects the full profile that C2 needs, and only 2,038 reached C2 against 2,611. In those 60 seconds the service completed 75,000 requests with the profiles, against 94,000 for plain java and 96,000 with the profiles turned off.

Why do so many methods stay at tier 2? When C2 is busy, the JVM compiles some methods at tier 2 to save work, and normally moves them on to tier 3 later, so they can collect a full profile and reach C2. With training data in the cache, that safety net is switched off. This is a known bug in JDK 25, and the same warm-up loss shows up in a separate test on 4 CPUs, so a later JDK 25 update may fix it. Until then, check warm-up on the JDK update you run. The results above are from JDK 25.0.5.

Two more details from the runs:

  • Training under real traffic helped on 1 CPU but not on 2. A cache trained with 60 seconds of requests served 8% more than plain java at 1 CPU, but still 19% fewer at 2 CPUs.
  • At a moderate steady rate the effect is smaller. At 300 requests per second, every setup settled within 10 seconds at 2 CPUs. At 1 CPU, Spring AOT with the AOT cache started fastest but had the worst latency in its first minute after ready (p99 of 4.8 s against 4.0 s for plain java). Counted from launch, though, both setups with the cache settled earlier than plain java, because they started so much sooner. They settled about 7 seconds earlier with the AOT cache alone, and 3 seconds earlier with Spring AOT added:
Line chart of p99 latency per 5-second window at 1 CPU and a steady 300 requests per second, plotted from JVM launch. All setups start with latencies of several seconds; the JDK AOT cache drops below 50 ms at about 39 seconds, Spring AOT with the AOT cache at about 43 seconds, plain java at about 46 seconds, and TieredStopAtLevel=1 at about 20 seconds apart from one later window.
p99 latency per 5-second window at 1 CPU and a steady 300 requests per second, plotted from JVM launch. Median of 5 runs.

None of this makes the AOT cache a bad choice. It halves startup, and for most services that matters more. It does mean that "startup time" is the wrong thing to measure alone. If you use the cache and new instances take full traffic at once, compare the first minutes under load with and without it, and treat -XX:-AOTReplayTraining as a diagnostic, not a production setting, because it is not a supported product flag.

On Kubernetes, this is the window in which the Horizontal Pod Autoscaler sees high CPU from new pods and may scale further. The Running Spring Boot in containers page shows the training run as a step in the Dockerfile.

Method

  • Machine: a cloud VM with 2 vCPUs and 7 GB of RAM, Linux 6.18. OpenJDK 25.0.5 (Ubuntu build 25.0.5.0.3), Spring Boot 4.1.2, Hibernate on in-memory H2.
  • CPU pinning: with taskset (a Linux command that runs a program only on the CPUs you choose), the JVM ran on 1 CPU with the load generator on the other, or on both CPUs shared with the load generator. The JVM sees the number of CPUs it is pinned to and sizes its compiler threads and GC from it, as it would in a container with a CPU limit.
  • Load: a small C load generator using epoll, sending a fixed mix that repeats every 20 requests: 8 reads by id, 4 paged list queries, 3 price queries, 4 order creations (a JPA write in a transaction, with validation) and 1 customer creation. In the steady-rate runs, latency is measured from the time each request was due to be sent, so time spent waiting in a queue counts.
  • Runs: 200 startup runs (10 per setup and CPU count), 100 steady-rate runs and 60 full-load runs (5 each), executed one at a time and round-robin across setups, so slow drift affects all of them alike. Every number is a median. The compiler statistics are from separate runs with -XX:+CITime (3 runs each).
  • Training: CDS and the AOT caches were each created by one training run on the same machine, with the same JDK and class path.

Recap

  • The JDK AOT cache gave the biggest startup gain, about half the time to ready, for one training run in the build. With Spring AOT on top, startup was almost 60% faster.
  • CDS cut startup by about a third and works on Java 21 and older.
  • It matters most where Java starts often, such as Kubernetes recovery and scaling, command-line tools, desktop applications, serverless and tests.
  • Lazy initialization made no difference for a web service, and TieredStopAtLevel=1 starts fast but loses over half of peak throughput, so keep it for tests and tools.
  • Under heavy load right after startup, check the first minutes as well as the startup time. On JDK 25 the cache's method profiles slowed warm-up in that case, and turning profile replay off removed it.