Video in progress
What is Application Class Data Sharing (AppCDS), and how does it speed up startup of a Spring Boot service?
AppCDS saves the internal representation of loaded and verified classes into an archive file, and on later startups maps them straight into memory. Parsing and verifying classes is skipped, so startup gets 20-40% faster.
📖 Answer
On every startup the JVM reads thousands of .class files from the jar, parses them, verifies the bytecode and builds structures in metaspace. In a Spring Boot application that can be 10-20 thousand classes. CDS does this work once and stores the result.
A CDS archive for the JDK's own classes has been on by default since Java 12. AppCDS adds your classes too:
# 1) training run: the application starts and the archive is written on exit
java -XX:ArchiveClassesAtExit=app.jsa -Dspring.context.exit=onRefresh -jar app.jar
# 2) prod start
java -XX:SharedArchiveFile=app.jsa -jar app.jar
Spring Boot 3.3+ has a convenient way for this: unpack the jar with java -Djarmode=tools -jar app.jar extract and do the training run above in the Dockerfile. With Buildpacks it is automatic with BP_JVM_CDS_ENABLED=true.
Limitations:
- the archive is tied to the exact JDK build and classpath. If the jar or the JDK changes, the archive isn't used (the JVM silently falls back to a normal startup, but with
-Xshare:onit fails); - the archive must be created inside the image, not in CI, otherwise the paths won't match;
- it doesn't solve JIT warm-up, only class loading.
If you need a bigger gain: in Java 24+ Project Leyden's AOT cache (-XX:AOTCache) stores classes in a loaded and linked state, and CRaC snapshots the warmed-up process itself. GraalVM native image gives the fastest startup, but with limits on reflection and dynamic proxies.
Sign up to mark studied questions and save your progress.