Boost Java Startup Speed with Class Data Sharing (CDS): A Step-by-Step Guide
This article explains what Java Class Data Sharing (CDS) is, provides detailed commands for training and using a CDS archive with a Spring Boot demo, and shows how startup time can be cut by nearly one second, helping developers choose the right Java startup optimization technique.
1. What is CDS?
Class Data Sharing (CDS) is a JVM feature that reduces Java application startup time and memory usage. Since JDK 12, the default CDS archive is packaged with the Oracle JDK binaries. The author tested with OpenJDK 64‑Bit Server VM Zulu21.34+19‑CA (build 21.0.3+9‑LTS), which also supports CDS.
2. How to use
2.1 Training
First run a training execution of the application in “extract” mode:
<code>$ java -Djarmode=tools -jar my-app.jar extract --destination application
$ cd application
$ java -XX:ArchiveClassesAtExit=application.jsa -Dspring.context.exit=onRefresh -jar my-app.jar
</code>This creates an
application.jsafile in the
applicationdirectory, which can be reused as long as the application is not updated.
2.2 Running with the archive
When starting the application, add the
-XX:SharedArchiveFileoption:
<code>$ java -XX:SharedArchiveFile=application.jsa -jar my-app.jar
</code>3. Results
The author generated a Spring Boot demo project and trained CDS. The directory structure after training is shown, followed by two execution scenarios.
3.1 Direct run
<code>$ java -jar demo-0.0.1-SNAPSHOT.jar
... (log output) ...
Started DemoApplication in 1.702 seconds
</code>3.2 Run with CDS
<code>$ java -XX:SharedArchiveFile=application.jsa -jar demo-0.0.1-SNAPSHOT.jar
... (log output) ...
Started DemoApplication in 0.722 seconds
</code>Using CDS reduces startup time by about one second compared with the direct run.
4. Summary
CDS, CRaC, and GraalVM all help improve Java startup speed, but they differ in use cases. CDS speeds up startup by sharing class metadata, CRaC optimizes runtime, and GraalVM provides AOT compilation. Developers can choose the appropriate technique based on their requirements.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.