Java Dynamic CDS: Enhancing JVM Startup Performance
Discover Java Class Data Sharing (CDS), introduced in JDK 5 to optimize JVM startup times. By utilizing a pre-processed archive of core classes and shared metadata, CDS streamlines the initialization process, allowing for faster application launches. Learn how to create and implement CDS archives to improve your Java application's performance.
Java - Dynamic CDS
What is CDS?
CDS stands for Class Data Sharing. Introduced in JDK 5, it aims to improve the startup time of the JVM by loading a pre-processed archive of core classes and shared JVM metadata. When the JVM initializes, it loads a set of core classes, for example, classes from the java.lang
package. Using CDS, Java can create a pre-processed archive of these core classes, allowing the JVM to skip the normal initialization process (expand archive, validate class, generate bytecode) and directly use the pre-processed archive. The following command can be used in JDK 5 and onwards to create a CDS archive for use by the JVM at startup:
$ java -Xshare:dump -cp APITester.jar APITester
The CDS archive will be located in the Java installation directory:
$JAVA_HOME/lib/server/classes.jsa
$JAVA_HOME/bin/server/classes.jsa
When the JVM initializes and is directed to use CDS, this archive will be used to load core classes instead of decompressing and verifying the classes, thus improving startup time.
What is Dynamic CDS?
Dynamic CDS is an enhancement of the original Class Data Sharing feature in the JVM to further boost application startup time. By allowing the sharing of class metadata across different JVMs, it reduces both startup time and memory footprint. Java 10 introduced AppCDS (Application Class Data Sharing), which provided developers with access to include application classes in a shared archive. In Java 12, the CDS archive was set as the default.
However, the process of creating a CDS was tedious, requiring developers to undergo multiple trial runs of their applications to create a class list as a first step and then dump that class list into an archive. From Java 13 onwards, Java introduced dynamic archiving, enabling developers to generate a shared archive at the time of application exit, eliminating the need for trial runs.
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
Create Dynamic CDS
The following steps showcase how to create a dynamic shared archive on top of the default system archive using the -XX:ArchiveClassesAtExit
option while passing the archive name:
$ java -XX:ArchiveClassesAtExit=sharedApp.jar -cp APITester.jar APITester
Once generated, the shared archive can be used to run the application using the -XX:SharedArchiveFile
option:
$ java -XX:SharedArchiveFile=sharedApp.jar -cp APITester.jar APITester
Example
Consider the following example:
APITester.java
public class APITester {
public static void main(String[] args) {
System.out.println("Welcome to TutorialsArena.");
}
}
Compile and Run the Program
$ javac APITester.java
$ jar cf APITester.jar APITester.class
$ java -XX:ArchiveClassesAtExit=sharedApp.jsa -cp APITester.jar APITester
$ java -XX:SharedArchiveFile=sharedApp.jsa -cp APITester.jar APITester
Output
Welcome to TutorialsArena.