Sub topic 11
Building the cache in CI and container images
This chapter assumes you can already run the record, create and run steps by hand. It also assumes you know that the JVM checks a cache against the JDK and classpath that built it. Here you will move those steps into a build pipeline and a container image, so every deployed artifact ships with a cache that matches it.
Why the cache belongs in the build
The AOT cache is a build artifact, like a compiled JAR. It is derived from three things:
- the exact JDK build
- the application's classpath
- the training run
If any of these changes, the cache must be rebuilt. This leads to three rules:
- Create the cache after the final application JAR exists, and never before.
- Create it with the same JDK that will run the application in production.
- Create it for the same operating system and CPU architecture as the target.
A cache built on a developer laptop (macOS, arm64) will not work in a Linux x64 container. The JVM rejects it, and you lose the startup gain without any obvious error.
A training run that works in a build environment
A CI runner is not production. It usually has no database, no message broker and no outbound network. The training run must still exercise the startup path, and it must exit on its own.
Most applications need a training mode: a flag or argument that starts the application, runs its normal initialization and a representative request or two, and then exits with status 0. Examples:
- A command-line tool can run a small built-in sample input.
- A web service can start, call its own health endpoint and a few typical routes, then shut down.
- A framework often has a "refresh the context and exit" switch. Check your framework's documentation for it.
Keep the training mode free of side effects. If it writes to a real database or calls a paid API, running it in CI will cause problems. Point it at in-memory or stub dependencies instead.
The JDK 24 pipeline: two commands
On JDK 24 you run record and create as separate commands. A script for a CI job might look like this:
#!/usr/bin/env bash
set -euo pipefail
APP_JAR=build/app.jar
CONF=build/app.aotconf
CACHE=build/app.aot
# 1. Record: run the training workload and write the configuration
java -XX:AOTMode=record -XX:AOTConfiguration="$CONF" \
-cp "$APP_JAR" Hello
# 2. Create: build the cache from the configuration
java -XX:AOTMode=create -XX:AOTConfiguration="$CONF" \
-XX:AOTCache="$CACHE" -cp "$APP_JAR"
ls -l "$CACHE"Two details matter here. The -cp value used at create time must match the value used at record time. The configuration file is an intermediate product, so you can discard it after the create step. You only deploy the .aot file.
The JDK 25 pipeline: one command
On JDK 25 and later, -XX:AOTCacheOutput runs the training workload and creates the cache in one command. The JVM still does the record and create work internally, using a temporary configuration file.
#!/usr/bin/env bash
set -euo pipefail
APP_JAR=build/app.jar
CACHE=build/app.aot
java -XX:AOTCacheOutput="$CACHE" -cp "$APP_JAR" HelloPrefer this form where you can. It removes the chance of passing different classpaths to the two steps.
Verifying the cache in CI
A cache file that exists is not proof that it works. Add a verification step to the job, and make it fail the build if the cache is unusable.
On JDK 25 you can use -XX:AOTMode=on, which makes the JVM exit with an error if it cannot use the cache. Combine it with class load logging to confirm that classes come from the cache:
#!/usr/bin/env bash
set -euo pipefail
APP_JAR=build/app.jar
CACHE=build/app.aot
# Fails immediately if the JVM rejects the cache
java -XX:AOTMode=on -XX:AOTCache="$CACHE" \
-Xlog:class+load -cp "$APP_JAR" Hello > build/classload.log
total=$(grep -c 'source:' build/classload.log)
cached=$(grep -c 'shared objects file' build/classload.log)
echo "classes loaded: $total, from cache: $cached"
# Require that at least 80 percent of classes came from the cache
if [ $((cached * 100)) -lt $((total * 80)) ]; then
echo "Cache coverage below threshold" >&2
exit 1
fiThe 80 percent threshold is an example. Run the check on a known good build, note the real ratio, and set the threshold slightly below it. A sudden drop usually means a dependency started loading classes the training run does not reach, or a library began generating classes at run time.
On JDK 24, AOTMode=on is not available, so the class load check is your main signal. You can also compare startup time against a baseline run without the cache, as in the measuring chapter.
Putting the cache in a container image
Containers are the most common place to use an AOT cache. The cache file has to sit inside the image, at a path the JVM can read at run time, and it has to match the JAR in the image.
The simplest reliable approach is to create the cache in the same image, with the same JDK, in a RUN step after the JAR is copied in:
FROM eclipse-temurin:25-jre
WORKDIR /app
COPY build/app.jar /app/app.jar
# Training run at image build time: writes /app/app.aot
RUN java -XX:AOTCacheOutput=/app/app.aot -cp /app/app.jar Hello
# Fail at container start if the cache cannot be used
ENTRYPOINT ["java", "-XX:AOTMode=on", "-XX:AOTCache=/app/app.aot", \
"-cp", "/app/app.jar", "Hello"]Why this layout works:
- The JDK that creates the cache is the JDK that runs the application, because both come from the same base image.
- The JAR path is the same at create time and run time (
/app/app.jar). The JVM checks the classpath, so moving the JAR would invalidate the cache. - The cache is created once during
docker build, not on every container start. Container start only reads it.
Multi-stage builds
Many images build the JAR in one stage and copy it into a smaller runtime stage. Create the cache in the final stage, after the copy:
FROM eclipse-temurin:25-jdk AS build
WORKDIR /src
COPY . .
RUN ./gradlew --no-daemon jar
FROM eclipse-temurin:25-jre
WORKDIR /app
COPY --from=build /src/build/libs/app.jar /app/app.jar
RUN java -XX:AOTCacheOutput=/app/app.aot -cp /app/app.jar Hello
ENTRYPOINT ["java", "-XX:AOTMode=on", "-XX:AOTCache=/app/app.aot", \
"-cp", "/app/app.jar", "Hello"]Do not create the cache in the build stage with the JDK image and copy it into the JRE image. Even if both are JDK 25, they are different builds with different contents, and the JVM may reject the cache. The same applies if you copy a cache between two images that use different base image tags.
Training with external dependencies
A RUN step in docker build has no access to your databases and often no network. Use the same training mode described earlier, with stubs or in-memory resources. If the training needs a configuration file, copy a training-specific one into the image for the RUN step. Production configuration then comes in at run time through environment variables or mounted files, which do not affect the classpath and so do not invalidate the cache.
Caching, reuse and multiple platforms
Cache creation adds seconds to a few minutes to a build. Keep these points in mind to control that cost and avoid reuse mistakes.
| Concern | Guidance |
|---|---|
| Rebuild trigger | Rebuild whenever the JAR, dependencies or JDK image change. A cache keyed on the JAR content hash plus the JDK version is safe. |
| Storing the file | Treat it as a build output. Do not commit it to version control. |
| Image size | The cache adds to the image size. Check `ls -l` on the file and weigh it against startup savings. |
| Multiple architectures | Build and train once per platform (for example linux/amd64 and linux/arm64). A cache from one architecture is not valid on the other. |
| Layer caching | Place the `RUN` that creates the cache after the `COPY` of the JAR. Docker then reruns it only when the JAR changes. |
| Runtime flags | Keep flags that affect the cache, such as the garbage collector choice, the same at create and run time where you can. Check the load log after changing them. |
If your CI system can run the image build on native runners for each architecture, prefer that. Emulated builds (for example, arm64 under QEMU on an x64 runner) run the training much slower and can time out.
Troubleshooting a pipeline that produces a bad cache
When the verification step fails or startup does not improve, work through these checks in order:
- Read the JVM output of the failing run. With
AOTMode=onon JDK 25, the error message states why the cache was rejected. - Compare the JDK version in the build and run stages with
java -version. They must be identical. - Compare the classpath strings. Relative versus absolute paths, or a different directory, count as different.
- Check that the training run reached your real startup path. The log should show your application's classes loading from the cache, not only JDK classes.
- Check the platform. Run
uname -min the build and run environments. - Check that no step modified the JAR after the cache was created.
Exercise
Your Dockerfile has a build stage on eclipse-temurin:25-jdk that builds app.jar and then runs java -XX:AOTCacheOutput=/build/app.aot -cp /build/app.jar App. The final stage uses eclipse-temurin:25-jre, copies both files to /app/, and starts the application with -XX:AOTCache=/app/app.aot -cp /app/app.jar. Startup is no faster than without the cache. Name two likely causes and give the fix.
Show answer
First, the classpath differs: the cache was created for /build/app.jar, but the container runs /app/app.jar. Second, the JVM that created the cache (the JDK image) is not the one that runs it (the JRE image), and the copied JAR may also have a different modification time. The fix is to create the cache in the final stage with RUN java -XX:AOTCacheOutput=/app/app.aot -cp /app/app.jar App after copying the JAR. Add -XX:AOTMode=on to the entrypoint so a rejected cache fails at start instead of being ignored.
Exercise
Why should a CI job check the share of classes loaded from the cache instead of only checking that the .aot file exists?
Show answer
The file can exist even when the training run exited early or did not reach the application's startup path, so it covers few classes. The file can also be rejected at run time because of a JDK, classpath or platform mismatch. The class load log shows how many classes actually come from the cache, so a threshold on that share catches both problems and fails the build.