Backend Development 9 min read

Boost Java Startup Speed with Class Data Sharing (CDS): A Step‑by‑Step Guide

This article explains what Java Class Data Sharing (CDS) is, walks through training and running a Spring Boot application with CDS, and demonstrates how startup time can be reduced by nearly one second, while comparing CDS with CRaC and GraalVM for Java performance optimization.

macrozheng
macrozheng
macrozheng
Boost Java Startup Speed with Class Data Sharing (CDS): A Step‑by‑Step Guide

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 binary. The author tested with OpenJDK 64‑Bit Server VM Zulu21.34+19‑CA (build 21.0.3+9‑LTS, mixed mode, sharing), which also supports CDS.

2. How to use

2.1 Training

First run a training execution of the application in exploded form:

<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

directory and generates

application.jsa

, which can be reused as long as the application is unchanged.

2.2 Running with CDS

When starting the application, add the

-XX:SharedArchiveFile

parameter pointing to the archive:

<code>$ java -XX:SharedArchiveFile=application.jsa -jar my-app.jar</code>

3. Results

Testing with a Spring Boot demo project shows the directory structure after training and the startup times.

3.1 Direct run

<code>$ tree application
application
|-- application.jsa
|-- demo-0.0.1-SNAPSHOT.jar
`-- lib
    |-- jackson-annotations-2.17.1.jar
    ... (other libraries) ...

1 directory, 32 files</code>

The log contains

Started DemoApplication in 1.702 seconds

, indicating a startup time of 1.702 seconds.

3.2 Run with CDS

After changing to the training directory and launching with the shared archive:

<code>$ java -XX:SharedArchiveFile=application.jsa -jar demo-0.0.1-SNAPSHOT.jar</code>

The log shows

Started DemoApplication in 0.722 seconds

, nearly one second faster than the direct run.

4. Conclusion

CDS, CRaC and GraalVM all help improve Java program startup speed, but they differ in application scenarios and optimization methods. CDS accelerates startup by sharing class data, CRaC optimizes runtime performance, and GraalVM achieves fast startup and efficient execution through AOT compilation. Developers can choose the appropriate technology based on their specific needs.

JavaJVMbackend developmentStartup PerformanceClass Data Sharing
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.