Class Data Sharing in Java
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 AppClassDataSharing.java 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.
Procedure:
This involves 3 steps:
- Record all the classes which you load while using your application in 1st file.
- Create a Shared Dump file for these classes.
- Use that Shared Dump later while launching the application.
Step 1: Recording all the classes
The above class named 'AppClassDataSharing.java' 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.
Similar Reads
Static class in Java Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class
3 min read
Dynamic Class Data Sharing in Java with Example In Java, a class is a template that defines the properties and behaviors of objects. Objects are instances of a class and can be created using the new keyword. A class typically consists of fields (also known as instance variables) and methods. Fields are variables that store data, and methods are b
3 min read
Sealed Class in Java In Java, we have the concept of abstract classes. It is mandatory to inherit from these classes since objects of these classes cannot be instantiated. On the other hand, there is a concept of a final class in Java, which cannot be inherited or extended by any other class. What if we want to restrict
2 min read
Local Inner Class in Java Prerequisites: Nested Classes in Java Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop or an if clause. Local Inner classes are not a member of any enclosing classes. They belong to the block the
5 min read
Nested Classes in Java In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation and creates more readable and maintainable code. The scope of a nested cl
5 min read
Java - Inner Class vs Sub Class Inner Class In Java, one can define a new class inside any other class. Such classes are known as Inner class. It is a non-static class, hence, it cannot define any static members in itself. Every instance has access to instance members of containing class. It is of three types: Nested Inner ClassMe
3 min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
Object Class in Java Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
7 min read
Types of Classes in Java A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien
8 min read