22

I read about CDS in Oracle doc https://docs.oracle.com/javase/8/docs/technotes/guides/vm/class-data-sharing.html

What I understood is the system class files needed for loading the jvm are parsed, verified and then stored in a archive at jre/lib/[arch]/client/classes.jsa. Moreover they also provide their memory mapping for jvm,so jvm directly maps the memory according to the mapping information given in the archive. So this reduces the overhead of class loading everytime a jvm instance starts. Please correct me if was wrong.

Now coming to java 10, how can I achieve this for my application code ? Secondly, would the complete application code be eligible for CDS or are there some restrictions?

Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32
  • 4
    "why don't we compile the complete application code to native code" - Are you familiar with Graal? – Jacob G. May 31 '18 at 21:12
  • @Jacob So now I read about graal and it is about ahead of time compilation. So it reduces the runtime compilation overhead. – Shubham Kadlag May 31 '18 at 21:21
  • 4
    CDS does not compile classes into native code. It rather preloads (parses, verifies, etc.) certain classes and stores them in a file in a binary form that allows direct mapping the image into the address space of JVM. – apangin May 31 '18 at 23:26
  • @apangin Thanks for the explanation. I now read more about CDS and I think I have a better idea now. I have edited the question now. Your inputs would be very much appreciated. – Shubham Kadlag Jun 01 '18 at 05:29

2 Answers2

19

There are three essential steps to creating and using an archive with application class-data (for more details, read my post about application class-data sharing):

  1. Creating a list of classes to include in the archive:

    java -XX:+UseAppCDS
        -XX:DumpLoadedClassList=classes.lst
        -jar app.jar
    
  2. Creating an archive:

    java -XX:+UseAppCDS -Xshare:dump 
        -XX:SharedClassListFile=classes.lst
        -XX:SharedArchiveFile=app-cds.jsa
        --class-path app.jar
    
  3. Using the archive:

    java -XX:+UseAppCDS -Xshare:on 
        -XX:SharedArchiveFile=app-cds.jsa
        -jar app.jar
    

Keep the following in mind:

  • you can’t use wildcards or exploded JARs for the class path when creating the archive
  • the class path used to launch the application must have the one used to create the archive as a prefix
  • if you have any problems, use -Xlog:class+load (more on -Xlog) to get more information
Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • On the first step I got only spring's and java classes. Missing classes from jar libraries. Tried manually add them into list, got error `Preload Warning: Cannot find com/example/SomeServiceClass`. How to properly add custom classes to classes.lst? – Woland Jun 24 '19 at 12:47
  • And what replacement for `--class-path` on java 1.8? – Woland Jun 24 '19 at 12:58
  • @Woland On Java 8, this was a commercial feature, right? I have no idea whether it works the same way. But on Java 8, it's `-classpath` or just `-cp`. – Nicolai Parlog Jun 25 '19 at 13:56
8

The JEP for AppCDS has the example show casing how to add your application classes to shared archive. As for the restrictions, there are few:

  1. Straight classes (.class) present in directory on class path cannot be added to the shared archive. See this thread.
  2. Classes loaded by custom class loaders cannot be added to the shared archive. See this thread.

There are other practical considerations to be aware of when using CDS/AppCDS, such as:

  1. If you update the jar files on the file system, then you will have to recreate the shared archive.
  2. If you are using Java or JVMTI agent(s) that modify/re-transform/redefine the class file at run-time, then the shared archive won't be useful as the classes will be loaded from the disk since the agents need actual classfile data which I believe is not stored in the shared archive.

Another nice and detailed article on CDS and AppCDS is https://simonis.github.io/cl4cds/.

Author of the article has also written a tool that allows sharing of application classes even if they get loaded by a custom class loaders.

If you are interested in using CDS, you can also try OpenJ9 JVM which has this feature for a long time and is much more mature and complete. Read more about it here.

Ashutosh
  • 181
  • 5