Sub topic 7
Measuring the improvement and checking that the cache is used
You have a cache file from the record, create and run workflow. This chapter covers two questions. First, is the application faster? Second, did the JVM actually use the cache, or did it quietly ignore it and run normally?
This chapter assumes you can compile and run a class or JAR, that you know what the classpath is, and that you have built app.aot for the Hello program from the previous chapter. The commands use the JDK 24 flags. JDK 25 adds a few conveniences, noted where they matter.
Why "it feels faster" is not enough
The JVM treats a cache it cannot use as an optimization, not as an error. If app.aot is missing, was built by a different JDK, or no longer matches the classpath, the default behavior is to print a warning at most and run without it. The program works, but it is slow. This makes two kinds of checks necessary:
- A timing check tells you how much faster the run is.
- A use check tells you why it is faster, or why it is not.
Do the use check first. A timing comparison is meaningless if the second run never read the cache.
Checking that classes come from the cache
The class+load log tag prints one line for every class the JVM loads, with the place it was loaded from. Run the application with the cache and keep the log in a file:
java -XX:AOTCache=app.aot -Xlog:class+load:file=load.log -cp app.jar HelloEach line looks like this:
[0.018s][info][class+load] java.lang.Object source: shared objects file
[0.031s][info][class+load] Hello source: shared objects fileWithout a usable cache, the application class reports its JAR file instead:
[0.052s][info][class+load] Hello source: file:/home/you/app.jarThe text after source: is the signal. The table lists the values you will meet.
| Source text | Meaning |
|---|---|
| `shared objects file` | The class came from the default JDK archive or from your AOT cache. |
| `shared objects file (top)` | The class came from a top-layer archive. You may see this with the older dynamic archive approach. |
| `jrt:/java.base` | The class was read from the JDK's runtime image and parsed, so it was not cached. |
| `file:/path/app.jar` | The class was read from a JAR on the classpath and parsed, so it was not cached. |
| `__JVM_LookupDefineClass__` or `__Lambda__` | A class generated at run time, such as a lambda proxy class. |
Counting cached and uncached classes
A single grep Hello line proves one class was cached. A count gives a better picture of the whole run:
echo "cached: $(grep -c 'source: shared objects file' load.log)"
echo "uncached: $(grep -vc 'source: shared objects file' load.log)"To see which classes missed the cache, list them:
grep -v 'source: shared objects file' load.log | head -20For the Hello program with the cache in use, the uncached list should be short. The default JDK archive already covers many core classes, so the AOT cache mostly adds the application's own classes and the classes recorded during training. A long uncached list that includes your own classes means the cache is not being used, or the training run did not exercise those classes.
Asking the JVM why it rejected the cache
When the counts look wrong, turn up logging for the archive code. On JDK 24 the tag is cds. Later releases also provide an aot tag, so try whichever your JDK accepts:
java -XX:AOTCache=app.aot -Xlog:cds=info -Xlog:class+path=info -cp app.jar HelloThe class+path tag prints the classpath the JVM compared against the one stored in the cache. A mismatch there is the most common reason for a silently ignored cache. The next chapter covers the exact validation rules. Here, you only need to know where the evidence appears.
Making a missing cache fail loudly
Logs are useful while debugging, but a deployment should not depend on someone reading them. JDK 25 adds -XX:AOTMode=on. With it, the JVM refuses to start if the cache cannot be used.
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar HelloIf the file is missing or rejected, the JVM exits with an error. The default mode, auto, continues without the cache. Use on in CI smoke tests and in production start scripts. The problem then shows up as a failed start, not as a slow one.
Measuring the startup improvement
A first measurement with `time`
The simplest comparison is the one from the spine:
time java -cp app.jar Hello
time java -XX:AOTCache=app.aot -cp app.jar HelloFor a program as small as Hello, the difference may be tens of milliseconds, and a single run is noisy. The JVM's first run after a build also pays disk cache costs that later runs do not. Two things make the numbers trustworthy: many repetitions, and the same conditions for both variants.
Repeating the measurement
This script runs each variant 20 times and reports the average wall-clock time in milliseconds. It discards one warm-up run for each variant, so both start with a warm file system cache.
#!/usr/bin/env bash
set -euo pipefail
RUNS=20
now_ms() { date +%s%3N; }
bench() {
local label="$1"; shift
"$@" > /dev/null # warm-up run, not counted
local total=0
for _ in $(seq "$RUNS"); do
local start end
start=$(now_ms)
"$@" > /dev/null
end=$(now_ms)
total=$(( total + end - start ))
done
echo "$label: $(( total / RUNS )) ms average over $RUNS runs"
}
bench "no cache " java -cp app.jar Hello
bench "with cache" java -XX:AOTCache=app.aot -cp app.jar HelloThe %3N format in date works with GNU date on Linux. On macOS, install coreutils and use gdate, or use the hyperfine tool, which handles warm-up, repetition and statistics for you:
hyperfine --warmup 3 \
'java -cp app.jar Hello' \
'java -XX:AOTCache=app.aot -cp app.jar Hello'Average only tells part of the story. If your tool reports minimum and standard deviation, look at them too. A large standard deviation means something else on the machine is disturbing the measurement.
Measuring time to first useful work
Wall-clock time for the whole process includes JVM shutdown, which you may not care about. For a server, the better question is how long it takes from process start to the moment it is ready. You can measure this inside the program. The JVM records its own start time, so the application can compare it with the current time:
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.stream.Collectors;
public class StartupTimer {
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);
long jvmStart = ManagementFactory.getRuntimeMXBean().getStartTime();
long elapsed = System.currentTimeMillis() - jvmStart;
System.out.println("Ready after " + elapsed + " ms");
}
}Build it and train the cache on this class exactly as before, then run it with and without -XX:AOTCache. This number excludes JVM shutdown and gives a cleaner comparison for services. For a real server, print the same value from the point where the server starts accepting requests.
Making the comparison fair
Several things can distort a before-and-after comparison:
- Different JVM flags. Run both variants with the same heap and garbage collector options. Some options also have to match the training run, which the next chapter explains.
- Cold and warm file cache. The first read of
app.aotcomes from disk. A warm-up run before measuring evens this out. If you care about cold-start behavior, such as a fresh container, measure it separately and say so. - Other load on the machine. Close busy processes, and on a laptop plug in the power supply. Shared CI runners are noisy, so repeat more times there.
- A different training run. If you retrain between measurements, you are comparing two different caches.
What to expect, and what not to
The cache moves class loading, parsing and linking work out of the run. It does not remove work done by your own code at startup, such as reading configuration, opening connections or running static initializers that were not recorded. It also does not make the JIT compiler warm up faster, which is a separate topic covered later in this tutorial.
The effect grows with the number of classes loaded. A tiny program has little to save. JEP 483 reports improvements of roughly 40 percent for a Spring Boot sample application, which loads thousands of classes. Treat that figure as an illustration of scale, and measure your own application.
If the gain is small, ask what the application is waiting on. Run with -Xlog:class+load and count the classes loaded. A few hundred classes means class loading was never the main cost.
A verification routine
Combine the checks into one routine you can run after every cache build:
flowchart TDThe diagram below shows the same routine as a decision flow.
A script version for CI, which fails when too few classes come from the cache:
#!/usr/bin/env bash
set -euo pipefail
MIN_CACHED=${MIN_CACHED:-100}
java -XX:AOTCache=app.aot -Xlog:class+load:file=load.log -cp app.jar Hello > /dev/null
cached=$(grep -c 'source: shared objects file' load.log || true)
echo "classes from cache: $cached"
if [ "$cached" -lt "$MIN_CACHED" ]; then
echo "AOT cache does not appear to be in use" >&2
exit 1
fiPick MIN_CACHED from a known-good run of your own application. Setting it to a fraction of that value, such as 80 percent, allows small changes without false alarms.
Common mistakes
- Timing a run that never used the cache. Always look at
class+loadbefore looking at the clock. - Comparing one run against one run. Single measurements vary by more than the gain for small programs.
- Checking only JDK classes. The default JDK archive already shares many core classes. A line for
java.lang.Objectfromshared objects filesays nothing about your cache. Check an application class. - Reading the log of the training or create step. The record and create steps run with different flags. Check the log of the final run step.
- Changing the JAR after building the cache. Rebuilding
app.jarcan invalidate the cache, so the next run looks slow for no apparent reason.
Exercise
You run java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello and the line for Hello ends in source: file:/home/you/app.jar, while java.lang.Object ends in source: shared objects file. Is the AOT cache being used for the application? What do you check next, and how would you prevent this from going unnoticed in production on JDK 25?
Show answer
No. java.lang.Object comes from the JDK's default archive, which is available without your cache. Hello was parsed from the JAR, so the cache was rejected or not found. Next, check that app.aot exists at the path given, that it was built with the same JDK, and that the classpath matches the one used at creation, using -Xlog:class+path=info and -Xlog:cds=info. To make it fail loudly on JDK 25, add -XX:AOTMode=on, so the JVM refuses to start when the cache is unusable.
Exercise
A teammate reports that the AOT cache made their service start 5 percent faster in a single run and wants to remove the cache step from the build. Name three things you would do before agreeing.
Show answer
Confirm with the class+load log that application classes load from the shared objects file, so the comparison is valid. Repeat the timing at least 20 times for each variant after a warm-up run, using the same JVM flags and similar machine load, and compare averages and spread. Measure time to readiness inside the application, and count loaded classes to see whether class loading is a meaningful part of startup. Also check that the training run exercised the startup path, because a poor training run produces a small gain.