Sub topic 12
Limitations and troubleshooting
This chapter assumes you can run the record, create and run workflow from the earlier chapters. You should also know how to read -Xlog:class+load output. Here you will learn what the AOT cache cannot do, how to find out why it is not used, and how to make a missing cache fail loudly.
What the cache does not do
The cache moves class loading, parsing and linking work out of every run. It does not change the rest of startup.
- Your application's own initialization still runs each time. Reading config files, opening connections and building large objects cost the same with or without the cache.
- The JIT compiler still has to warm up. The cache stores class data, not optimized machine code. Method profiles are a separate topic, covered in the profiling chapter.
- Only classes seen during the training run are cached. A class that the training run never loaded is loaded the normal way.
- Classes from custom class loaders are generally not cached. Classes from the boot, platform and application loaders are the ones the cache targets.
A cache can only help with the work it holds. If your startup time is dominated by your own code, a smaller gain is expected.
Why a cache gets rejected
The JVM checks the cache against the current run. If a check fails, the JVM prints a warning and, by default, carries on without the cache. The application still works, but it starts at the old speed. That makes silent rejection the most common problem.
Common causes:
| Cause | What to check |
|---|---|
| Different JDK | The cache must be run by the same JDK build that created it. Rebuild after any JDK update. |
| Different platform | A cache made on one OS or CPU architecture does not work on another. |
| Changed classpath | The runtime classpath must match the training classpath. Same JARs, same order, and the JAR files must be unchanged. |
| Rebuilt JAR | A new build changes file size or timestamp, so the old cache no longer matches. |
| Incompatible options | Some JVM options differ between training and production, such as module options or the garbage collector. |
The rule of thumb is to create the cache from the same artifacts, in the same environment, that you will run in production.
A troubleshooting routine
Work through the checks in this order.
1. Check the source of the classes
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep HelloA class loaded from the cache reports a shared objects file as its source. A class loaded from app.jar was not served by the cache.
2. Read the cache log
java -XX:AOTCache=app.aot -Xlog:cds -cp app.jar HelloLook for warnings about a mismatch. They usually name the check that failed, such as the classpath or the JDK version.
3. Turn silent failure into an error
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar HelloWith AOTMode=on, the JVM refuses to start if it cannot use the cache. Use this in tests and in staging so a stale cache fails a deployment instead of slowing every start. The flag is available in JDK 25 and later, as shown in the one-step chapter.
4. Improve training coverage
If the cache is accepted but few classes come from it, the training run did not exercise your startup path. Run the training with realistic input. Make it exercise the code that runs at startup, and let it exit cleanly. Then record and create the cache again.
Operational limits
- The cache file is tied to one JDK build, so every JDK upgrade needs a rebuild.
- Cache files can be large, because they hold class data for everything the training run loaded. Account for this in image size.
- A training run that fails or exits early gives a cache with little in it.
- Keep the training and production command lines as close as possible. Changing options between steps is a frequent cause of rejection.
Exercise
You deploy a new release of your application, and startup time returns to its old value. The java command still passes -XX:AOTCache=app.aot, and the application runs without errors. What is the most likely cause, and how do you find out and prevent it in future?
Show answer
The release has rebuilt JARs, so the old cache no longer matches the classpath (size or timestamp). The JVM rejects it and runs without the cache. Find out with -Xlog:cds (rejection warning) or -Xlog:class+load (classes load from the JAR, not a shared objects file). Prevent it by rebuilding the cache in the same pipeline as the JAR, and by running with -XX:AOTMode=on in testing so a stale cache fails loudly.