Sub topic 3
From Class Data Sharing to the AOT cache
This chapter assumes you can compile and run a Java program from the command line and package it as a JAR. It also assumes you know what the classpath is and know that the JVM loads classes on demand. You don't need to know how the JVM stores classes internally. This chapter explains that as it goes.
The AOT cache did not appear from nothing. It extends Class Data Sharing (CDS), a feature the JDK has shipped for many years. If you know how CDS and its application variant (AppCDS) work, the AOT cache is easy to place. It also explains why the cache has the limits it has.
The problem CDS was built to solve
When a class is first needed, the JVM does several things:
- It finds the
.classbytes, either in a JAR or in the JDK's runtime image. - It parses the bytes into the JVM's internal class representation.
- It verifies the bytecode.
- It links the class and later initializes it.
A small program that uses streams and lambdas touches hundreds of JDK classes before main prints anything. Every run repeats the same work for the same classes, and the result is always identical.
CDS addresses this. It does the parsing and verification once, writes the resulting internal class data to an archive file, and maps that file into memory on later runs. Mapping a file is much cheaper than reading, parsing and verifying hundreds of classes. Because the mapped pages are read-only, several JVM processes on one machine can also share the same physical memory. That sharing gives the feature its name.
How CDS evolved
CDS grew in steps. Each step widened what could be archived or made the archive easier to produce.
| JDK | Feature | What changed |
|---|---|---|
| 5 | Original CDS | An archive of core JDK classes, created at JDK install time. |
| 10 | AppCDS (JEP 310) | Application classes on the class path could be archived too. |
| 12 | Default CDS archive (JEP 341) | The JDK build ships an archive of JDK classes, so CDS works without setup. |
| 13 | Dynamic archiving (JEP 350) | `-XX:ArchiveClassesAtExit` archives the classes a run actually loaded. |
| 19 | Auto archive | `-XX:+AutoCreateSharedArchive` creates or refreshes a dynamic archive automatically. |
| 24 | AOT cache (JEP 483) | The cache stores classes in a loaded and linked state. |
| 25 | Command-line ergonomics (JEP 514), method profiling (JEP 515) | Simpler cache creation, and cached profiles for JIT warmup. |
AppCDS in practice
Before moving on, look at the older workflow. It shows what the AOT cache simplified. This example uses the Hello program from the earlier chapters and the app.jar built from it.
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);
}
}The dynamic archive approach from JDK 13 needs one training run and one normal run:
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.class
# Run once and write an archive of the loaded classes when the JVM exits
java -XX:ArchiveClassesAtExit=app.jsa -cp app.jar Hello
# Run with the archive
java -XX:SharedArchiveFile=app.jsa -cp app.jar HelloThe static approach gives you more control. It dumps a list of the classes a run loads, then builds an archive from that list:
# 1. Record the names of loaded classes
java -XX:DumpLoadedClassList=classes.lst -cp app.jar Hello
# 2. Build a static archive from that list
java -Xshare:dump -XX:SharedClassListFile=classes.lst \
-XX:SharedArchiveFile=app.jsa -cp app.jar
# 3. Run with the archive
java -XX:SharedArchiveFile=app.jsa -cp app.jar HelloBoth approaches share the idea behind the AOT cache. A training run observes which classes the application uses, a separate step turns that observation into an archive file, and production runs map the file.
What CDS saves and what it leaves behind
CDS removes the cost of finding the class bytes, parsing them, and verifying the bytecode. It does not remove all the work between "class bytes exist" and "class is ready to use". Some work still happens in every run:
- The JVM still has to load each class: it creates the class object, registers it with its class loader, and resolves its superclasses and interfaces. Class loading from an archive is cheap, but it is not free, and it happens one class at a time as the program asks for each class.
- Linking is only partly done. Verification is skipped, but resolving symbolic references in the constant pool still happens lazily, on first use.
- Lambdas and method handles are expensive in an unarchived run. The first use of a lambda makes the JVM generate a hidden class at runtime. In early CDS, the generated classes were not part of the archive, so this cost stayed. Later releases archived the lambda proxy classes for the common case.
These leftovers are small per class, but they add up in an application with thousands of classes, such as a framework that scans the class path at startup.
What the AOT cache adds
The AOT cache (JEP 483, "Ahead-of-Time Class Loading & Linking") is a delivery vehicle for the same idea with a larger scope. It is part of Project Leyden, an OpenJDK effort whose goal is to improve startup time, time to peak performance and footprint by moving work from run time to earlier phases.
The key change is the state of the classes. The cache stores classes as they look after loading and linking, so at startup the JVM can make them available as already loaded and linked, instead of going through the load-and-link sequence on demand. The same archive format also holds more of the work that follows from linking, such as pre-resolved constant pool entries and the linkage for lambdas, which depends on what the training run observed.
Consider what that means for the Hello example. In a plain run, the stream pipeline and String::toUpperCase cause the JVM to load and link dozens of classes, and to spin up hidden classes for the method reference. With the cache, most of that work was done once, at cache creation, and the run starts from the result.
The JEP reports an improvement of roughly 40 percent in startup time for a Spring-based sample application. Treat that as an order of magnitude for framework-heavy programs. A tiny program like Hello spends more of its time on JVM initialization, and its improvement is smaller in absolute terms. You will measure this yourself in a later chapter.
What the cache is not
It helps to be precise about the boundaries, because they follow from the design:
- The cache is not compiled machine code. Despite the name "AOT", JDK 24 does not store native code from the JIT compiler. The work moved ahead of time is class loading and linking. Method profiles arrive with JEP 515 in JDK 25, and they help the JIT compiler warm up faster, but they are profile data, not compiled methods.
- The cache is not a native image. The program still runs on the full JVM, with dynamic class loading, reflection and the JIT compiler available. If something is not in the cache, the JVM loads it the normal way. This is a major difference from GraalVM Native Image, which gives up that flexibility.
- The cache is tied to what it was trained on. It reflects the classes the training run used, so a class the training run never touched is loaded the slow way.
From a class list to a configuration
In AppCDS, the training run's output was a plain list of class names. The AOT workflow keeps the same shape with a richer record. In JDK 24, the record step writes an AOT configuration file, and the create step reads it to build the cache. The configuration holds more than names, because it also has to describe how classes were linked and which lambda call sites were used.
That is why the new workflow has an explicit AOTMode setting, with the record, create and on values. The JDK 24 commands look like this:
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -cp app.jar Hello
java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot -cp app.jar
java -XX:AOTCache=app.aot -cp app.jar HelloThe next chapters cover each step, the training run's design, and the JDK 25 shortcut that merges the first two steps.
Compatibility is inherited from CDS
Because the AOT cache descends from CDS, it inherits CDS's validation rules. An archive holds data in the JVM's internal layout, so it is only valid for the JVM that wrote it. In practice, the JVM checks things such as the JDK version and build, and it checks that the class path used at run time matches the one used at creation. If a check fails, the JVM does not use the cache. By default it prints a warning at most, then continues without the cache. This silent fallback is the most common source of confusion, and a later chapter shows how to make it fail loudly.
Choosing between the options
For new work on JDK 24 or later, use the AOT cache. The older options are still the right choice in a few cases:
- On JDK 21 or an older release, the AOT cache is not available, so use
-XX:ArchiveClassesAtExitor the static dump workflow. The-XX:+AutoCreateSharedArchiveoption, available from JDK 19, builds the archive on first run, which suits environments where you cannot add a build step. - When you only care about sharing memory between many JVMs, any CDS archive helps. The default JDK archive already covers the JDK classes.
- When you want the largest startup gain on JDK 24 or later, the AOT cache is the one that stores loaded and linked classes.
Exercise
A team runs a Spring service on JDK 21 and uses -XX:ArchiveClassesAtExit. They plan to move to JDK 24. Name two things the AOT cache stores or does that the CDS dynamic archive does not, and name one thing neither of them stores.
Show answer
The AOT cache stores classes in a loaded and linked state, so the JVM does less load-and-link work at startup, and it records more of the linkage work, such as lambda call sites, that follows from the training run. Neither stores compiled machine code from the JIT compiler. The JVM still compiles hot methods at run time. In JDK 25 the cache can also hold method profiles (JEP 515), but those are profile data, not compiled code.
Exercise
You build an AOT cache and then upgrade the JDK on the production machines without rebuilding it. The application still starts, but no faster than before. What is the likely reason, and why did you get no error?
Show answer
The cache was written by a different JDK build, so the JVM's validation rejected it. A rejected cache falls back to normal class loading by default instead of failing, so the program runs without the cache and without an error. Rebuild the cache with the new JDK. You can also use -XX:AOTMode=on so a missing or unusable cache stops the JVM from starting.