Sub topic 4
What the AOT cache stores
This chapter assumes you can compile a class and run it from a JAR, and that you know the JVM loads classes on demand and JIT-compiles hot methods. The previous chapters covered where startup time goes and how Class Data Sharing (CDS) works. Here you will see what the AOT cache adds to it.
The cache in one sentence
The AOT cache is a single file that holds your application's classes after the JVM has already loaded, verified and linked them. On later runs the JVM maps that file into memory instead of repeating the work. JEP 483 introduced it in JDK 24 as part of Project Leyden.
It extends CDS and AppCDS. Those archived a class's parsed form. The AOT cache also archives the results of work that normally happens after parsing.
What goes into the file
The cache holds four kinds of data.
| Content | What it saves at startup |
|---|---|
| Parsed class data from the JDK, your JAR files and libraries | Reading and parsing `.class` files |
| Verification and linking state | Running the bytecode verifier and preparing the class for use |
| Resolved constant pool entries | Looking up the classes, fields and methods that bytecode refers to by name |
| Archived heap objects, such as class mirrors and some strings | Building those objects on every run |
Resolved references matter most for programs like the Hello example. Its String::toUpperCase method reference and its stream pipeline use invokedynamic call sites. Linking a lambda normally runs bootstrap code that generates classes at run time. The cache can store the outcome of that linkage, so the JVM skips much of that work.
What it does not store
The cache removes class loading and linking costs. It does not remove everything.
- JIT-compiled machine code. JDK 24 does not store compiled code. The JVM still compiles hot methods on each run. JDK 25 adds stored method profiles (JEP 515), which help warmup. A later chapter covers them.
- Your application's own initialization. Opening sockets, reading configuration and starting frameworks all run every time. If your
mainmethod spends two seconds connecting to a database, the cache does not shorten that. - Classes the training run never used. The cache only contains what the recording step saw. Other classes load the usual way. The next chapter covers how to choose a training run.
- Application state. The cache holds classes and a small set of JDK-managed objects. It does not hold your program's objects or the values of your static fields from the training run.
Checking where a class came from
The JVM can tell you whether a class came from the cache. The class+load log prints a source for each class.
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep -E "Hello|java.util.stream.Collectors"A class served from the cache reports shared objects file as its source. A class loaded the normal way reports its JAR path or jrt:/ module location. If your own classes show a JAR path while JDK classes show shared objects file, the cache was used but did not cover your classes. That usually means the cache was built from a different classpath than the one you are running with.
Common mistakes
- Expecting the cache to speed up long-running work. It reduces startup and early warmup. A service that runs for hours sees little change in steady-state throughput.
- Assuming the cache holds compiled code. It does not in JDK 24. Early requests can still be slow while the JIT catches up.
- Building the cache from a trivial run. If the training run only prints a version string, the cache contains few of your classes.
- Treating a silent fallback as success. By default the JVM runs without the cache if it cannot use it. Check the class source as shown above instead of relying on the absence of errors.
Exercise
A Spring-style application takes 4 seconds to start. Profiling shows 1.5 seconds in class loading and linking, and 2.5 seconds in the application's own initialization code (reading configuration, opening connections). Roughly how much can the AOT cache save, and why?
Show answer
At most about 1.5 seconds. The cache stores loaded, verified and linked classes, resolved constant pool entries and some archived heap objects, so it removes the class loading and linking time. The 2.5 seconds of application initialization runs on every start and is not stored in the cache. In practice the saving is somewhat smaller than 1.5 seconds, because classes missing from the training run still load normally.