Sub topic 4
What the AOT cache stores
This chapter assumes you can compile a class and package it into a JAR, and that you know the JVM loads classes on demand and compiles hot methods with the JIT. You don't need to know the cache workflow yet. The next chapters cover how to build one.
The short version
The AOT cache is a single file. It holds the result of the class loading and linking work the JVM would otherwise repeat on every start. The file contains classes in their parsed, verified and linked form. It also contains some pre-resolved references and a snapshot of a few heap objects. It does not contain compiled machine code.
The cache is a superset of the older Class Data Sharing (CDS) archive. It is defined by JEP 483, "Ahead-of-Time Class Loading & Linking", which shipped in JDK 24 as part of Project Leyden. Leyden's goal is to move work from run time to an earlier step. Here that work is class loading and linking.
What the JVM does without a cache
Without a cache, each class the application touches goes through these steps on every start:
- Find the bytes in a JAR or the runtime image.
- Parse the class file into the JVM's internal metadata.
- Verify the bytecode.
- Link the class and resolve symbolic references on first use.
- Run the static initializer.
A program that uses streams, lambdas and method references pulls in hundreds of JDK classes before main does anything useful. A framework application pulls in tens of thousands. The cost of each class is small, but the total is large and it is paid again on every run.
What the cache stores
The table lists the main kinds of content and what each one saves at startup.
| Content | What it is | What it saves |
|---|---|---|
| Loaded classes | Class metadata for JDK classes and for application classes from the class path, already parsed | Reading and parsing class files |
| Linked state | Classes already verified and prepared for use | Bytecode verification and linking |
| Resolved references | Some constant pool entries already resolved, including lambda and string concatenation call sites | First-use resolution work |
| Archived heap objects | A snapshot of selected Java objects, such as `Class` mirrors and some strings | Creating and initializing those objects |
| Profile data (JDK 25 and later) | Method profiles from the training run | Part of the JIT warmup |
The last row belongs to a later chapter. The sections below cover the first four.
Loaded and linked classes
The cache keeps the JVM's internal representation of each class. At run time the JVM maps that data into memory instead of reading and parsing .class files. Because the data was verified when the cache was created, the JVM doesn't verify it again. The same trick worked in older CDS archives for JDK classes. The AOT cache applies it to application classes as well.
A class enters the cache only if the training run loaded it. A class the training run never touched is not in the cache and is loaded the normal way later. That is why the training run matters, and why a later chapter is devoted to it.
Resolved references
Compiled bytecode refers to other classes, fields and methods by name. The JVM resolves each name the first time that instruction runs, which means a lookup and some checks. Lambdas are the heaviest case. The first execution of a lambda expression goes through an invokedynamic call site. That call site spins up a hidden class through the LambdaMetafactory.
When the cache is created, the JVM can store the result of some of this resolution. That includes the generated lambda proxy classes. At run time the call site is already connected, so the first execution of String::toUpperCase in the example below costs much less than it would in a cold JVM.
Archived heap objects
Some objects exist in every run of the JVM. Examples are the java.lang.Class object for each loaded class and a set of interned strings. The cache can store a snapshot of selected objects from the Java heap. At run time the JVM brings this snapshot into the heap instead of building the objects one by one. How the snapshot is loaded depends on the garbage collector and the JDK version, so treat that as an internal detail. You don't configure it.
What is not in the cache
The cache doesn't remove every startup cost. These items are outside it:
- Compiled code. The JIT still compiles hot methods during the run. JEP 483 does not store native code. JDK 25 adds stored method profiles, which help the JIT decide earlier, but the compiled code is still produced in each run.
- Static initializer results in general. A class's static initializer runs again unless the JVM has specifically archived that class's state. Initializers that open files, read configuration or start threads always run.
- Application work. Parsing configuration, creating beans, opening connections and warming caches in your own code all happen in every run.
- Classes the training run didn't load. They load normally.
- Some classes that can't be archived. These include classes from custom class loaders that the JVM can't reproduce, and classes that fail verification. Classes generated at run time by arbitrary libraries are another case. Later chapters cover these limits.
The JEP reports a gain of roughly 40 percent for a Spring PetClinic example. That is a large application whose startup time is dominated by class loading. A small program that spends its time elsewhere gains less. Which fraction of your startup is class loading decides how much the cache helps.
The cache is one file with strict requirements
The cache is a single file, for example app.aot. The JVM maps it into memory at startup. The data inside is specific to the JVM that built it, so the cache only works when these things match:
- The same JDK build, including vendor and version.
- The same CPU architecture and operating system.
- A compatible class path. The class path at run time must start with the class path used at creation, and the JARs must be unchanged.
- Compatible JVM settings for some options, such as the garbage collector choice in certain cases.
If a check fails, the JVM does not use the cache. By default it continues without it and prints no error, so the application starts slowly and you may not notice. A later chapter shows how to make that case fail loudly with -XX:AOTMode=on.
Seeing what is in the cache
You can check what the cache holds by logging class loading. Use the program from the tutorial's running example.
import java.util.List;
import java.util.stream.Collectors;
public class Hello {
public static void main(String[] args) {
List<String> names = List.of("ada", "grace", "linus");
String result = names.stream()
.map(String::toUpperCase)
.collect(Collectors.joining(", "));
System.out.println(result);
}
}After you have built app.aot with the record, create and run steps, compare the log with and without the cache.
# Without the cache: classes come from the JDK image or the JAR
java -Xlog:class+load -cp app.jar Hello | grep -E "Hello|Collectors"
# With the cache: the source column changes
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep -E "Hello|Collectors"Each line names the class and its source. Without the cache, Hello reports the JAR path and Collectors reports a JDK location. With the cache, both report shared objects file. Count the lines that name the shared file against the total to see how much of the startup the cache covers.
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello \
| grep -c "shared objects file"
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello \
| grep -vc "shared objects file"The first number is the classes served from the cache. The second counts every other line, including log lines that are not class loads. A class that loads from somewhere else is a class the training run missed or the JVM could not archive.
What this means for your decisions
Two practical consequences follow from the contents of the cache.
First, the training run decides what is stored. The cache records the classes the run loaded and the references it resolved. It is not a copy of your whole class path. A training run that only prints --help produces a cache that helps little. A run that performs the real startup path produces a cache that covers it.
Second, the cache removes class loading work but not application work. If a profile shows that your startup spends most of its time in your own initialization code, fix that code. The cache won't help there. If the profile shows many thousands of classes loading, the cache is a good fit.
Exercise
A team builds an AOT cache for a web service. The training run only starts the server and immediately exits. In production, the first request to the /report endpoint is still slow because it loads many classes. Why, and what should change?
Show answer
The cache contains only the classes the training run loaded. Because the training run never handled a /report request, the classes for that path are not in the cache and load normally on first use. The fix is to make the training run exercise that path, for example by sending a representative request to /report before the process exits, and then rebuild the cache.
Exercise
Which of these does the AOT cache from JDK 24 store: verified class metadata, JIT-compiled native code, resolved lambda call sites, results of your own static initializers that read a config file?
Show answer
It stores verified class metadata and some resolved call sites, including archived lambda proxy classes. It does not store JIT-compiled native code, and it does not store the results of your own initializers that read a config file. That code runs again on every start.
Exercise
You run the application with -XX:AOTCache=app.aot and the startup time doesn't improve. Name two checks that use only what you learned in this chapter.
Show answer
First, run with -Xlog:class+load and check whether the classes report shared objects file as their source. If they don't, the cache isn't being used or the classes aren't in it. Second, compare the JDK build, architecture and class path with the ones used to create the cache. A mismatch makes the JVM ignore the cache silently.