Skip to content
Related Articles

Related Articles

Class Data Sharing in Java

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 03 May, 2022

Here we will be discussing one of the features introduced as an ailment in Java10. Here we will be discussing one of the features named class data sharing popularly known as CDS which can help reduce the startup time and memory footprints for Java applications.

Note: It helps to reduce the start-up time and memory footprints between Java Virtual Machines(JVM) 

When you use the installer to install the Oracle Java Runtime Environment (JRE), the installer loads a default set of classes from the system Java Archive (JAR) file into a private internal representation and dumps that representation to a file called a shared archive. If the JRE installer is not being used, then you can generate the shared archive manually. When the JVM starts, the shared archive is memory-mapped to allow sharing of read-only JVM metadata for these classes among multiple JVM processes. Because accessing the shared archive is faster than loading the classes, startup time is reduced.

Class data is supported with the G1, serial, parallel, and parallelOldGC garbage collectors. The shared string feature (part of class data sharing) supports only the G1 garbage collector on 64- bit non-Windows platforms. The primary motivation for including CDS in Java SE is to decrease in startup time. The smaller the application relative to the number of core classes it uses, the larger the saved fraction of startup time.

If JRE is installed using the installer and go to the path jre/bin/[server or client], you will see a file classes.jsa

Why shared Archive file(classes.jsa) is important?

This file is important because it contains a dump of the converted form of many System classes. Since these java classes are some of the System classes that are loaded every time JVM start-up and their content doesn’t change, unlike your own application code. The dump of these classes is taken once when you install JRE and converted into easy to load form and is used again and again improving JVM startup time bypassing many steps of usual class loading which would happen again and again every time on launching JVM.

Creating shared archive file by Ourself

Let’s suppose classes.jsa file is not present, you can create it yourself using the command java -Xshare:dump. This command will go to classlist file and check what classes to load and create a dump of it. One can try deleting the jre/bin/[client or server]/classes.jsa file and recreate it using the command java_Xshare:dump. 

In the below snapshot classes.jsa file has been deleted and in the next, we recreated it using java-Xshare:dump

If we delete the JRE/lib/classlist file, this process will give an error as it will not know what classes to load and dump. In the below snapshot, we have changed the name of the classlist file and tried taking a dump which throws an error

How to check if classes are getting loaded from Share Archive file or jar files?

We will run a simple program to see classes getting loaded from classes.jsa file and not from their respective jars, the VM arguments that I will supply to my program will be:- verbose – Xshare:on (-Xshare: on means switch on Class Data Sharing). I have created the below simple program and ran it with the above args.

You can see many System classes getting loaded from the Shared Object file. I have also added simple Math.random() in code to show a class java.lang.Math$RandomNumberGeneralHolder which is not loaded from the Shared object file since it is not part of Shared Archive and is loaded from rt.jar.

If we use args -verbose – Xshare:off, which means a switch of Class Data Sharing then the result obtained is as follows:

What is ‘Application’ Class Data Sharing?

In the above example, we saw some classes like Math$RandomNumberGeneratorHolder were loaded from the actual source and our own standalone class was also loaded from the actual source. What if we could also dump them in Shared Archive and use that Shared Archive in the future which will improve the runtime of our application. This is what Application Class Data Sharing is that we can use CDS from Application classes as well.


This involves 3 steps: 

  1. Record all the classes which you load while using your application in 1st file.
  2. Create a Shared Dump file for these classes.
  3. Use that Shared Dump later while launching the application.

Step 1: Recording all the classes 

The above class named ‘‘ in a runnable jar AppCDS.jar.(as AppCDS doesn’t dump flat classes). So to create a last file I will use the below command as follows:

java -XX:+UnlockCommercialFeatures-XX:+UseAppCDS -XX:DumpLoadedClassList=LoadedClasses.1st -jar AppCDS.jar

The file is been created on the custom directory so here we are not interfering with JRE files.

We can see in the above snapshot that a newly created file LoadedClasses. First and we can also see in the below snapshot it also has entry from its own class ‘AppClassDataSharing.’

Step 2: Creating a shared dump file for the above classes

Using this 1st file we will create dump at the same location and further below is the command and args to create Custom Shared Archive File:

java -XX:+UnlockCommercialFeatures -Xshare:dump -XX:+UseAppCDS -XX:SharedClassListFile=LoadedClasses.1st -XX:SharedArchiveFile=CustomSharedArchive.jsa -cp AppCDS.jar

Note: CustomSharedArchive.jsa file has been created and next we will use it while launching our application to load classes from it.

Step 3: Use the Shared Dump while launching the application.

We will be launching our app using the below command and params to use CustomSharedArchive.jsa file’

java -XX:UnlockCommercialFeatures -verbose -XShare:on -XX:+UseAppCDS -XX:SharedArchiveFile=CUstomSharedArchive.jsa -jar AppCDS.jar

We can see that both files RandomNumberGeneratorHolder and AppClassDataSharing are now loaded from shared object file now. You can try above command using – Xshare:off to see the results.

We can use the time command(just prefix time in front of command) to see the difference when you use _Xshare:on vs -Xshare:off. 

So with Application Class Data Sharing, we can reduce the footprint and runtime of our application.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!