Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - Dynamic CDS



What is CDS?

CDS stands for Class Data Sharing. It was introduced in JDK 5 to improve the startup time of the JVM by loading the pre-processed archive of core class and shared JVM Metadata. When JVM Initializes, it loads set of core classes, for example, java.lang package classes. Using CDS, Java supports creating a pre-processed archive of such core classes so that the normal process of initialization (expand archive, validate class, generate bytecode) can be improved by directly using pre-processed archive. Following command can be used in JDK 5 onwards to create a CDS archive to be used by JVM at startup time.

$java -Xshare:dump -cp APITester.jar APITester

The CDS archive will be available in JAVA installation directory.

$JAVA_HOME/lib/server/classes.jsa
or
$JAVA_HOME/bin/server/classes.jsa

When JVM is initialized and directed to use CDS, this archive will be used to load core classes instead of decompressing and verifying the classes thus improving the startup time.

What is Dynamic CDS?

CDS, Class Data Sharing is an important feature of JVM to boost the startup time of an application loading. As it allows to share class metadata across different JVMs, it reduces the startup time and memory footprint. Java 10 enhanced CDS by giving AppCDS, application CDS which gave developers access to include application classes in a shared archive. Java 12 set CDS archive as default.

But the process of creating a CDS was tedious as developers has to go through multiple trials of their applications to create a class list as first step and then dump that class list into an archive. Then this archive can be used to share metadata between JVMs.

From Java 13 onwards, now java has dynamic archiving. Now developers can generate a shared archive at the time of application exit. So trial runs are no more needed.

Create Dynamic CDS?

Following step showcases to create a dynamic shared archive on top of default system archive using option -XX:ArchiveClassesAtExit and 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 -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 TutorialsPoint.");
   }   
}

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 TutorialsPoint.
Advertisements