Sub topic 8
When the JVM accepts or rejects a cache
This chapter assumes you can build the cache with the record, create and run steps, and that you know what a classpath is. It covers how the JVM decides at startup whether a cache file is usable, and what it does when it is not.
The cache is a snapshot of one environment
The AOT cache holds data derived from specific class files and from a specific JVM. If either changes, the data could be wrong. For example, a cached class might no longer match the bytes in the JAR. So the JVM checks the cache before using it.
The checks fall into three groups:
| Group | What must match | Typical way to break it |
|---|---|---|
| The JVM | The same JDK build, on the same operating system and CPU architecture | Creating the cache with one JDK and running with a patch update or another vendor's build |
| The application | The classpath and the JAR files on it | Rebuilding the JAR, or reordering classpath entries |
| JVM options | Options that change object layout or memory handling | Training with one heap or compressed-pointer setting and running with another |
The exact list of options that must agree differs between JDK releases. Treat "same JDK, same flags" as the safe rule.
How the classpath is checked
The classpath check surprises people most often. When the cache is created, the JVM records the classpath and, for each JAR, its size and last-modified time. At run time it compares them as follows:
- The run-time classpath must start with the training classpath, in the same order. Entries added at the end are allowed.
- Each JAR must have the same size and modification time as when it was recorded. The JVM does not hash the contents.
- The cache is built from JAR files. A classpath entry that is a directory of loose class files is not a usable source of cached classes.
The modification-time rule means a rebuild invalidates the cache even if the code did not change. Copying a JAR with a tool that resets timestamps does the same.
What happens on rejection
By default the JVM treats a bad cache as an optimization it can skip. It prints a warning, or nothing at all, and starts normally by loading classes from the JAR. The program works, but startup is slow again. A cache that was valid yesterday can silently stop working after a deploy.
The -XX:AOTMode option controls this:
autois the default. The JVM uses the cache if it is valid and runs without it otherwise.onmakes the JVM exit with an error if the cache cannot be used.
Seeing a rejection
This script creates a cache, rebuilds the JAR so its timestamp changes, and then runs with the stale cache. It reuses the Hello program from earlier chapters.
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.class
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
# Rebuild the JAR. The contents are the same, but the modification time is new.
sleep 2
jar --create --file app.jar --main-class Hello Hello.class
# auto mode: starts, but without the cache
java -XX:AOTCache=app.aot -Xlog:cds -Xlog:class+path=info -cp app.jar Hello
# on mode: refuses to start
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar Hello
echo "exit status: $?"In the first run, read the cds and class+path log lines for a message about a mismatch in the JAR's size or modification time. The exact wording differs between JDK versions. In the second run the JVM prints an error and the exit status is not zero.
Habits that keep the cache valid
- Create the cache after the final JAR is built, as the last build step, and never edit or touch the JAR afterwards.
- Create and run with the same JDK build. Pin the JDK version in your build image.
- Pass the same classpath string at training and run time. Keep the order and use the same paths.
- Check the log output after changing the JDK, the base image, or the build tooling.
Exercise
You create an AOT cache in CI, copy app.jar and app.aot into a container image with a command that resets file timestamps, and startup is slow again. The application still runs correctly. What is the most likely cause, and how would you make the problem visible?
Show answer
The copy changed the JAR's modification time, so it no longer matches the value recorded in the cache. In the default auto mode the JVM then skips the cache and runs normally, which is why nothing fails. To see it, run with -Xlog:cds and -Xlog:class+path=info and look for the mismatch message. To make it fail loudly, add -XX:AOTMode=on. To fix it, create the cache inside the image after the JAR is in place, or copy in a way that keeps timestamps.