Sub topic 7
Measuring the improvement and checking that the cache is used
This chapter assumes you can build the cache with the record, create and run steps from the previous chapter. It also assumes you can run shell commands and read JVM log output. Here you will answer two questions: is the program faster, and is it really using the cache?
Why you need both checks
A JVM that cannot use a cache does not stop. By default it prints a warning at most, then runs normally. A faster run can also come from a warm disk cache or a quiet machine. A timing alone does not prove the cache worked, and a log alone does not prove a gain. Do both.
Timing the two runs
Run the same program with the same classpath, with and without -XX:AOTCache. Repeat each run several times, because the first run after a build is often slower. The loop below uses the Bash time keyword, so it is not portable to other shells.
TIMEFORMAT='%R s'
echo "Without cache"
for i in 1 2 3 4 5; do time java -cp app.jar Hello > /dev/null; done
echo "With cache"
for i in 1 2 3 4 5; do time java -XX:AOTCache=app.aot -cp app.jar Hello > /dev/null; doneDiscard the first result of each group, then compare the rest. Redirecting output to /dev/null keeps terminal speed out of the measurement.
The Hello program is tiny. Its saving is only a few tens of milliseconds, and that is close to the noise on a busy machine. Larger applications that load thousands of classes show much bigger gains. When you measure your own service, time the point you care about, such as the first request being served, and not only process exit.
Checking where classes come from
The class+load log prints one line for every class the JVM loads, including the source it loaded from.
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep HelloWith a working cache, the line for Hello names a shared objects file as its source. Without the cache, the source is the path of app.jar.
To see the effect across all classes, count how many came from each source:
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello > with.log
java -Xlog:class+load -cp app.jar Hello > without.log
grep -c 'source: shared objects file' with.log without.log
grep -c 'source: file:' with.log without.logThe JDK ships with a default Class Data Sharing archive, so most JDK classes already show shared objects file even without your cache. The difference you are looking for is in your own classes and in the classes your application pulls in on top of the JDK. Those should move from the file: count to the shared count.
Making a missing cache fail loudly
If the cache file is missing or rejected, a plain run falls back to normal startup. That is safe in production, but in a build or test it hides mistakes. Set -XX:AOTMode=on to make the JVM refuse to start when it cannot use the cache:
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar HelloUse this in your test or benchmark runs. A timing taken from a run that quietly ignored the cache would be meaningless. If the run fails, the error message tells you the cache was not usable. The next chapter covers the reasons.
Common measuring mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Timing one run | Disk cache and CPU frequency distort the result | Repeat and discard the first run |
| Changing the classpath between runs | The JVM may reject the cache | Use the same `-cp` as in the create step |
| Rebuilding `app.jar` after creating the cache | The cache no longer matches the JAR | Re-run record and create |
| Not checking the log | You may measure a run without the cache | Check `class+load`, or use `AOTMode=on` |
Exercise
You run java -XX:AOTCache=app.aot -cp app.jar Hello and it is no faster than a run without the flag. Name two checks that tell you whether the cache was used, and what result each should show if it was.
Show answer
First, run with -Xlog:class+load and check that the application classes report a shared objects file as their source instead of the JAR path. Second, run with -XX:AOTMode=on. If the cache is usable the program starts normally, and if it is not usable the JVM exits with an error. A cache that is used but gives no gain suggests the training run did not cover the classes that matter.