Java 13

Java 13 – Dynamic CDS archive

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.

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

Once generated the shared archive can be used to run the application using -XX:SharedArchiveFile option.

>java -XX:SharedArchiveFile=sharedApp.jar -cp Java13Archive.jar Java13Archive

Example

Consider the following example −

Java13Archive.java

public class Java13Archive {
   public static void main(String[] args) {
      System.out.println("Welcome to Hi Techpoints.");
   }   
}

Output

Compile the class using javac compiler as follows −

>javac Java13Archive.java

>jar cf Java13Archive.jar Java13Archive.class

>java -XX:ArchiveClassesAtExit=sharedApp.jsa -cp Java13Archive.jar Java13Archive

>java -XX:SharedArchiveFile=sharedApp.jsa -cp Java13Archive.jar Java13Archive

It should produce the following output −

Welcome to Hi Techpoints.

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs