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.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
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 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.jar

This 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.jar

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

$ java -jar demo-0.0.1-SNAPSHOT.jar
... (log output) ...
Started DemoApplication in 1.702 seconds

3.2 Run with CDS

$ java -XX:SharedArchiveFile=application.jsa -jar demo-0.0.1-SNAPSHOT.jar
... (log output) ...
Started DemoApplication in 0.722 seconds

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaStartup PerformanceCDSspring-bootClass Data Sharing
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

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.