Sub topic 12
Limitations and troubleshooting
This chapter assumes you can build and run an AOT cache with the record, create and run steps. It also assumes you know how to read -Xlog output and what a classpath is. Here you learn what the cache cannot do, how to find out why a cache was not used, and how to fix the usual causes.
What the cache does not do
The AOT cache moves class loading and linking work out of each run. Everything else about JVM startup stays the same. The limits fall into three groups.
Costs the cache does not remove
- JVM initialization. Setting up the heap, threads, and the runtime still happens on every start.
- Your own startup code. Reading configuration, opening connections, and running static initializers that do real work all still run. The cache stores class metadata, not the results of your code.
- JIT compilation. In JDK 24, the cache does not store compiled code. The JIT compiler still has to warm up. Cached method profiles are covered in the earlier chapter on JEP 515.
- Classes the training run never loaded. A class missing from the training run is loaded the normal way, from the JAR, with parsing, verification, and linking.
If a service spends most of its startup waiting on a database, the cache gives you little. Measure first, as described in the measuring chapter.
Classes that are not cached
The cache covers classes loaded by the built-in loaders: the boot, platform, and application class loaders. Some classes are left out, and the JVM skips them silently:
- Classes from custom class loaders that you or a framework define. Plugin systems, application servers, and some frameworks load code this way.
- Classes that fail verification or use a very old class file format.
- Classes generated at run time, for example by bytecode libraries or dynamic proxies. These do not exist as files, so they cannot be recorded in a way that matches later runs. Lambda forms and some other JDK-generated classes are an exception. The JDK knows how to archive them.
This means a framework-heavy application may see a smaller gain than the small Hello example. The JVM does not report each skipped class by default. You will see the effect as classes that still load from the JAR.
Runtime features that can interfere
Java agents and JVMTI agents can change class bytes as classes load. Because a cache holds the bytes seen during training, an agent in the production run can disable parts of the cache, or the JVM can refuse the cache. Treat the agent set as part of the cache's identity. Use the same agents in training and production, or none.
Why a cache is rejected
The JVM validates the cache at startup. Chapter 8 of this tutorial covers the rules in detail. The common failures are summarized here because they cause most troubleshooting.
| Cause | What happened | Fix |
|---|---|---|
| Different JDK | The cache was built by another JDK version or build, or on another OS or CPU architecture. | Build the cache with the same JDK binary that runs production. |
| Classpath mismatch | The production `-cp` does not start with the training `-cp`, or entries appear in a different order. | Use the identical classpath, or extend it only by appending entries. |
| JAR changed | A JAR was rebuilt after training. The JVM checks each JAR's size and modification time. | Rebuild the cache after every build of the JARs. |
| Directory on classpath | The classpath contains a directory of `.class` files instead of JARs. | Package the code as JARs for training and production. |
| Incompatible options | A JVM option or agent in production conflicts with what was recorded. | Compare the option sets and remove the difference. |
| Corrupt or missing file | The cache file was truncated, copied partly, or not copied at all. | Re-copy the file and check its size. |
By default, the JVM does not stop when it rejects a cache. With -XX:AOTMode=auto (the default), it runs normally without the cache. Your application works, but it is slow again. That is why a silent failure is easy to miss.
A troubleshooting workflow
Work through the checks in order. Each check narrows the cause.
Step 1: make a missing cache fail loudly
Run with -XX:AOTMode=on. The JVM then exits with an error if it cannot use the cache, instead of continuing without it.
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar Hello
echo "exit code: $?"A non-zero exit code and an error message mean the cache was rejected. Keep AOTMode=on in staging and test environments. In production, many teams prefer the default so that a bad cache never prevents a service from starting. Whichever you choose, make it a deliberate decision.
Step 2: read the validation message
Enable logging for the cache subsystem and for class paths. Names of the log tags can differ slightly between releases, so check java -Xlog:help on your JDK.
java -XX:AOTCache=app.aot -Xlog:aot -Xlog:class+path=info -cp app.jar HelloLook for lines that mention a mismatch. A classpath problem typically produces a message such as shared class paths mismatch, with a hint to enable -Xlog:class+path=info. That log shows the expected and the actual entries side by side. A different-JDK problem produces a message that the archive was created by a different version or build of HotSpot.
Step 3: confirm classes come from the cache
If the JVM starts but the speedup is small, check where classes load from. Cached classes report shared objects file as their source.
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello > load.log
echo "from cache:"
grep -c "shared objects file" load.log
echo "from jar or other source:"
grep -vc "shared objects file" load.logThe second count is not zero even in a healthy run, because the JVM can create some classes at run time. What matters is whether your own classes appear in the second group. List them like this:
grep -v "shared objects file" load.log | grep -E "source: (file|jar)"Classes listed here were loaded from a JAR or file in this run. Compare the list with your training run. A class that appears here and was used in training points to a skipped class (custom loader, generated class, or agent). A class that appears here and was never used in training points to a gap in the training run.
Step 4: close gaps in the training run
The training run should exercise the code paths that production uses in its first seconds. Common gaps:
- The training run only calls
--helpor exits early, so most classes never load. - A web application is trained without sending any requests, so request handling classes are missing.
- Optional features are switched off in training and on in production.
- Training runs on a smaller configuration, so different implementation classes are selected.
A good training run starts the application, performs a few representative operations, and shuts down cleanly. Make it a script, so the same run happens on every build. A training run should not need production secrets or external services. If it does, use a test profile that still loads the same code.
Common problems and fixes
The cache works locally and fails in the container
The JDK in the image is not the JDK that built the cache, or the JAR path differs. A cache records classpath entries, so building at /build/app.jar and running at /app/app.jar can fail validation. Build the cache inside the final image, or use the same absolute paths in both places. The CI chapter shows the layout.
The cache stops working after a rebuild
Every build produces JARs with new modification times, even when the code is unchanged. The cache then fails the JAR check. Regenerate the cache as part of every build step that produces the JAR, and never publish a cache without the JAR it was built from.
The training run hangs or the create step fails
The record step runs your application until it exits. A server that never exits will never finish recording. Give the training run a way to stop itself, for example a timer or a shutdown request after the test operations finish. If the create step fails, check that the configuration file from the record step exists, and that you pass the same classpath to both steps.
The cache file is large
The file holds class data for every loaded class, so it grows with the size of your application. A cache of tens of megabytes is normal for a large application. It is memory-mapped at startup, so the size matters mostly for image size and download time rather than for runtime memory.
Behavior differs with a different garbage collector
Parts of the cache hold archived heap objects. Support for different collectors has improved across releases. If you change the collector, test that the cache is still accepted, and rebuild it if the log reports a problem.
A checklist before shipping
- Build the cache with the exact JDK and JARs that you ship.
- Run with
-XX:AOTMode=onin at least one test environment, so that a bad cache fails the test. - Check the
class+loadlog for your main application classes. - Time the cached and uncached runs on the target hardware, several times each.
- Store the cache next to the JAR, and treat them as one artifact.
Exercise
A service starts with -XX:AOTCache=app.aot and no error, but startup is no faster. List three checks you would run, in order, and what each one tells you.
Show answer
First, restart with -XX:AOTMode=on. If the JVM now exits with an error, the cache is being rejected (different JDK, classpath mismatch, changed JAR), and the default mode was hiding that by running without the cache. Second, run with -Xlog:class+load and look at the source: field of your application classes. If they do not say shared objects file, the cache is not serving them, so read the -Xlog:aot and -Xlog:class+path=info output for the reason. Third, list the classes that still load from the JAR and compare them with the training run. Classes that were never used in training indicate a gap in the training run. Classes that were used in training but still load from the JAR indicate skipped classes, for example from a custom class loader or changed by an agent. If everything loads from the cache and startup is still slow, the time is going to work the cache does not remove, such as your own initialization code.
Exercise
A team builds the cache in a CI job at /work/app.jar, then copies app.jar and app.aot into a container image at /app/app.jar. The cache is rejected at startup. Give one likely cause and one fix.
Show answer
The classpath recorded in the cache is /work/app.jar, while the container runs with /app/app.jar, and the JVM's classpath validation fails. Also, copying the JAR can change its modification time, which fails the JAR check. The fix is to create the cache inside the final image (or an identical environment), with the same paths and the same JAR file, after the JAR is in its final place.