A Java application may start quickly on a developer's machine but take noticeably longer to become responsive after deployment. This is common in services that load many classes, initialize frameworks, connect to databases, and prepare application components during startup.
The problem becomes more visible in environments where applications start and stop frequently. Containerized services, serverless workloads, and applications that scale automatically may repeatedly pay the cost of class loading and runtime warm-up.
The Java Virtual Machine traditionally addresses runtime performance through Just-In-Time compilation. The JIT compiler observes application behavior while the application runs and compiles frequently executed code into optimized native machine code. This approach provides strong runtime optimization, but the JVM needs time to collect information before some optimizations become effective.
The newer Ahead-of-Time (AOT) capabilities in the JDK help reduce some of this startup and warm-up work. The AOT cache introduced in JDK 24 allows applications to reuse information collected during an earlier run. JDK 25 also simplifies the process of creating these caches and introduces ahead-of-time method profiling improvements.
These capabilities do not mean that every Java application is converted into a standalone native executable. Instead, they help the HotSpot JVM prepare selected application information before the next execution.
This article explains how AOT caching works, how to create and use an AOT cache, how to integrate it into a deployment workflow, and what developers should validate before using it in production.
Understanding Ahead-of-Time Compilation in the JDK
To understand the new AOT capabilities, it helps to distinguish between traditional JIT compilation and AOT optimizations.
With traditional JIT compilation, the JVM starts by loading and interpreting application code. As the application runs, the JVM collects information about frequently executed methods and uses that information to optimize performance.
This process is effective for long-running applications, but short-lived applications may finish their work before receiving the full benefit of runtime optimization.
AOT caching moves selected preparation work into an earlier execution phase.
The JVM can collect information about the application during a training run and store reusable artifacts in an AOT cache. During a later execution, the JVM can load that cache and avoid repeating some of the preparation work.
The important difference is that AOT caching complements the existing JVM execution model. The application still runs on the JVM, and JIT compilation remains available for runtime optimization.
For example, a Spring-based service may load hundreds of classes during startup. If the application is trained using a representative execution, the resulting cache can contain information that helps the JVM load and link those classes more efficiently during subsequent runs.
AOT caching is particularly interesting for applications where startup time and early responsiveness are important operational requirements.
Understanding the AOT Cache Workflow
The AOT cache workflow has three main phases:
<CodeBlock language="mermaid" id="92747"> flowchart TD A["Training run"] --> B["Collect application information"] B --> C["AOT configuration data"] C --> D["Cache assembly"] D --> E["AOT cache file"] E --> F["Production application startup"] F --> G["Reuse cached artifacts"] G --> H["JIT continues runtime optimization"] </CodeBlock>
Training
During training, the application runs with a representative workload.
The JVM observes the application and records information that can be used to build an AOT cache. This information is stored in an AOT configuration file or passed through the streamlined cache-generation workflow.
A representative workload is important. If training only exercises the application's health endpoint, the cache may not contain useful information about other parts of the application, such as report generation, authentication, or database-backed operations.
Assembly
During assembly, the JVM uses the collected information to create the AOT cache.
The application itself does not need to execute its normal business workload during this phase. The JVM processes the recorded information and creates the cache file that can be reused later.
Production
During production execution, the JVM loads the generated cache.
If the cache is compatible with the application and runtime environment, the JVM can reuse the stored information to improve startup and warm-up behavior.
The cache is not a replacement for the application JAR. It is an additional artifact that must be deployed alongside the application.
Creating an AOT Cache With JDK 25
One of the useful improvements in JDK 25 is simpler command-line handling for AOT cache creation.
With the streamlined workflow, developers can use the AOTCacheOutput option to perform the training and cache assembly process through a single command.
Consider a Java application packaged as application.jar.
Run the following command:
java -XX:AOTCacheOutput=application.aot \
-jar application.jar
This command starts the application in a training run. The JVM records the information needed for the AOT cache and then performs the cache assembly step.
The resulting application.aot file can be used during subsequent application launches.
However, the training run must exercise the application sufficiently to produce useful information. For a long-running service, the training process must receive the required workload and then be shut down so that cache creation can complete.
A command that starts a service and leaves it running indefinitely will not complete the cache-generation workflow until the training process exits.
The workload should include important application paths, such as:
Application startup and dependency initialization.
Common API endpoints.
Authentication and authorization flows.
Database access.
Frequently used business operations.
Important background processing tasks.
Do not use production customer data simply to generate a training cache. Use controlled test data that exercises the same application paths without exposing sensitive information.
Running the Application With the Generated Cache
Once the AOT cache has been created, pass it to the JVM during application startup.
java -XX:AOTCache=application.aot \
-jar application.jar
The JVM attempts to load application.aot and reuse its cached artifacts.
If the cache is compatible, the application can benefit from the preparation work completed during training.
For production environments, the default automatic AOT mode is generally safer than forcing the cache to be mandatory. In automatic mode, the JVM can continue without the cache if it cannot load it.
This is important because a cache is an optimization, not a requirement for the application's correctness.
For example, an application should not become unavailable simply because a deployment accidentally includes a cache generated for a different JDK release or architecture.
You can also enable AOT logging when diagnosing cache loading:
java -XX:AOTCache=application.aot \
-Xlog:aot \
-jar application.jar
The logging output helps developers understand whether the JVM can use the cache and why it may reject incompatible artifacts.
Understanding AOT Cache Compatibility
An AOT cache is not a universal file that can be copied between any Java application environments.
The cache is tied to important characteristics of the application and runtime.
These include:
If these characteristics change, the cache must be recreated.
For example, a cache generated for an application running on one JDK release should not automatically be reused after upgrading the application to another JDK release.
Similarly, a cache generated for an application running on one CPU architecture should not be assumed to work on a different architecture.
This has direct implications for containerized deployments.
Suppose a team builds an application image on one machine and deploys it to a different infrastructure environment. The application JAR may remain unchanged, but the runtime architecture or JDK distribution may differ.
The team should generate and validate the cache for the actual deployment environment rather than assuming that the cache is portable.
Application packaging also matters. If the class path, module path, or application contents change, the cache may no longer match the application.
A practical approach is to generate the AOT cache as part of the application build or image preparation process and deploy it together with the exact application artifact for which it was generated.
Using the AOT Workflow With Spring Boot
Spring Boot applications often perform significant startup work while creating the application context, loading configuration, discovering components, and initializing dependencies.
Spring Boot provides guidance for using the JDK AOT cache with supported Java runtimes.
For an application packaged as an executable JAR, the application can first be extracted into a directory:
java -Djarmode=tools \
-jar my-app.jar \
extract \
--destination application
Move into the extracted application directory:
cd application
Then create the AOT cache:
java -XX:AOTCacheOutput=app.aot \
-Dspring.context.exit=onRefresh \
-jar my-app.jar
The Spring Boot property allows the training process to exit after the application context has been refreshed, rather than continuing to run as a normal long-lived service.
After cache generation, start the application using the cache:
java -XX:AOTCache=app.aot \
-jar my-app.jar
For this workflow, use the extracted application form consistently. The cache must be used with the application layout for which it was generated.
This approach is useful for containerized Spring Boot services where reducing startup overhead can improve deployment and scaling behavior.
However, not every application has the same initialization requirements. Applications with scheduled tasks, external integrations, or important startup-dependent components should validate that the training process exercises the required initialization paths.
Understanding Ahead-of-Time Method Profiling
AOT caching and AOT method profiling are related improvements, but they address different parts of application startup and warm-up.
AOT caching can reuse information such as loaded and linked classes and heap objects.
JDK 25 also introduces ahead-of-time method profiling improvements. This allows method execution profiles collected during an earlier application run to be available when the JVM starts.
The JVM can use these profiles to make better-informed JIT compilation decisions earlier in the application's execution.
For example, suppose an API service frequently executes a method responsible for validating incoming requests. During a training run, the JVM observes how the method behaves.
In a subsequent run, the previously collected profile information can help the JIT compiler make optimization decisions without waiting to collect the same information from scratch.
This does not mean that every method is automatically compiled into native code before the application starts. The JVM still uses its runtime compilation mechanisms, but previously collected profiling information can help it begin optimization earlier.
Developers should therefore evaluate startup time and warm-up behavior separately. AOT caching may improve class loading and linking, while method profiling can help the JVM make earlier optimization decisions.
Integrating AOT Cache Generation Into CI/CD
AOT cache generation should be treated as part of the application build and deployment process.
A practical pipeline can follow this sequence:
Build the Java application.
Run automated tests.
Prepare the application using the target JDK.
Execute a representative training workload.
Generate the AOT cache.
Validate that the cache can be loaded.
Package the application and cache together.
Deploy the package to a test environment.
Compare startup and warm-up behavior against the normal JVM execution.
The cache should be associated with the exact application build and runtime environment.
For example, a deployment package can contain:
application/
application.jar
application.aot
runtime-info.txt
The runtime information can record the JDK release, operating system, architecture, and application build identifier used during cache generation.
This makes it easier to identify compatibility problems when a deployment fails to load the cache.
If the application changes, generate a new cache rather than reusing an artifact from an older build without validation.
Also consider the resources required during cache creation. Training and cache assembly consume CPU and memory, and the streamlined workflow may launch an additional JVM process for assembly. Run cache generation in an environment with sufficient resources instead of placing it on a resource-constrained production instance.
Measuring Whether AOT Actually Helps
AOT should be introduced based on measured improvements rather than assumptions.
Compare the application under two conditions:
Measure more than the time required for the Java process to start.
Useful measurements include:
Time until the application is ready to accept requests.
Time required for the first successful API request.
CPU usage during startup.
Memory consumption during startup.
Response time during the initial workload.
Time required to reach normal operating performance.
Use the same machine type, JDK release, application build, configuration, and workload for both tests.
Run multiple measurements because startup time can vary due to operating system caching, container initialization, storage performance, and external service availability.
Also separate application initialization time from dependencies such as database availability or remote service latency. AOT cannot eliminate delays caused by an unavailable database or a slow external API.
The objective is to identify whether the JVM startup and warm-up work is a meaningful part of the application's performance problem.
Common Mistakes to Avoid
AOT caching is useful, but an incorrect implementation can provide little benefit or create deployment problems.
One common mistake is generating a cache without exercising the important application workflows. The cache may then provide limited improvements for the actual production workload.
Another mistake is copying the cache between different runtime environments. The cache is specific to the application and runtime characteristics, so it should be regenerated when those characteristics change.
Some teams also assume that AOT caching eliminates JIT compilation. It does not. The JVM continues to use its normal runtime optimization mechanisms.
Finally, avoid treating successful cache generation as proof of better application performance. Always compare startup, readiness, warm-up, and resource consumption using representative workloads.
Conclusion
The JDK's Ahead-of-Time capabilities provide another way to improve Java application startup and warm-up behavior without abandoning the traditional JVM execution model.
The AOT cache introduced in JDK 24 allows applications to reuse information collected during an earlier execution. JDK 25 makes cache generation simpler through improved command-line ergonomics and adds ahead-of-time method profiling improvements that help the JVM optimize application code earlier.
For developers, the practical workflow is straightforward: train the application with representative workloads, generate a compatible cache, deploy it with the application, and validate the results.
The biggest gains come from applying AOT to applications where startup and early responsiveness matter. Measure the results, regenerate caches when the application or runtime changes, and keep normal application execution independent of the cache. This allows teams to benefit from AOT optimizations without making the cache a production reliability dependency.
Join the conversation! Your thoughts help the community grow.