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:
$ java -Djarmode=tools -jar my-app.jar extract --destination application
$ cd application
$ java -XX:ArchiveClassesAtExit=application.jsa -Dspring.context.exit=onRefresh -jar my-app.jarThis creates an application.jsa file in the application directory, 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:SharedArchiveFile option:
$ java -XX:SharedArchiveFile=application.jsa -jar my-app.jar3. 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
$ java -jar demo-0.0.1-SNAPSHOT.jar
... (log output) ...
Started DemoApplication in 1.702 seconds3.2 Run with CDS
$ java -XX:SharedArchiveFile=application.jsa -jar demo-0.0.1-SNAPSHOT.jar
... (log output) ...
Started DemoApplication in 0.722 secondsUsing 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
