CDS (Class Data Sharing) is a way to optimize JVM for fast startup.

1 A Simple App

Assume the simple application

Copy
public class HelloJava {
    public static void main(String... args){
        System.out.println("Hello Java!");
    }
}

Compiled with javac HelloJava.java and ran it measuring it's time

Copy
$ time java HelloJava
Hello Java!
java HelloJava  0.11s user 0.02s system 117% cpu 0.113 total

2 Generating the share dump

Class Data Sharing is a JVM feature which is intended to remove the fixed cost of loading core classes at startup. It also allows some cached files to be shared between JVMs which helps if you’re running a lot of them. This is cool because it can be made to work even if the JVMs are containerised.

CDS is easy to set up, just run with -Xshare:dump to generate the cache.

Copy
⇒ java -Xshare:dump
Allocated shared space: 50577408 bytes at 0x0000000800000000
Loading classes to share ...
Loading classes to share: done.
Rewriting and linking classes ...
Rewriting and linking classes: done
Number of classes 1192
    instance classes   =  1178
    obj array classes  =     6
    type array classes =     8
Updating ConstMethods ... done. 
Removing unshareable information ... done. 
ro space:   5355536 [ 30.5% of total] out of  10485760 bytes [ 51.1% used] at 0x0000000800000000
rw space:   5627928 [ 32.1% of total] out of  10485760 bytes [ 53.7% used] at 0x0000000800a00000
md space:    136544 [  0.8% of total] out of   4194304 bytes [  3.3% used] at 0x0000000801400000
mc space:     34053 [  0.2% of total] out of    122880 bytes [ 27.7% used] at 0x0000000801800000
st space:     12288 [  0.1% of total] out of     12288 bytes [100.0% used] at 0x00000007bff00000
od space:   6372368 [ 36.3% of total] out of  20971520 bytes [ 30.4% used] at 0x000000080181e000
total   :  17538717 [100.0% of total] out of  46272512 bytes [ 37.9% used]

This creates the file $JAVA_HOME/lib/server/classes.jsa.

3 Running with CDS

We know that the CDS cache has been used, because Xshare:on will fail if the cache isn’t found. We can also check where the classes are loaded from with the argument -Xlog:class+load=info which was previously known as -XX:+TraceClassLoading.

With CDS we see output like this:

Copy
⇒ java -Xshare:on -Xlog:class+load=info HelloJava
[0.004s][info][class,load] opened: /home/mjg/tools/jdk-9/lib/modules
[0.014s][info][class,load] java.lang.Object source: shared objects file
[0.014s][info][class,load] java.io.Serializable source: shared objects file
[0.014s][info][class,load] java.lang.Comparable source: shared objects file
[0.015s][info][class,load] java.lang.CharSequence source: shared objects file
...etc...

and without CDS:

Copy
⇒ java -Xlog:class+load=info HelloJava
[0.003s][info][class,load] opened: /home/mjg/tools/jdk-9/lib/modules
[0.014s][info][class,load] java.lang.Object source: jrt:/java.base
[0.014s][info][class,load] java.io.Serializable source: jrt:/java.base
[0.014s][info][class,load] java.lang.Comparable source: jrt:/java.base
[0.015s][info][class,load] java.lang.CharSequence source: jrt:/java.base
...etc...