diff --git a/README.md b/README.md index 02ee85d..64167f6 100644 --- a/README.md +++ b/README.md @@ -4,28 +4,117 @@ ##Building: -``javac src/com/gc/example/GCExample.java -d `pwd`; jar cvfm GCExample.jar src/com/gc/example/manifest.txt com/gc/example/GCExample.class;`` +``javac src/com/cds/example/CDSExample.java -d `pwd`; jar cvfm CDSExample.jar src/com/cds/example/manifest.txt com/cds/example/CDSExample.class;`` -##Running with GC options (JDK 11): +##Running with CDS options (JDK 8): -``rm -f `pwd`/gc*; java -Xms50m -Xmx50m -Xlog:gc*=debug:file=`pwd`/gc.log:tags,time:filecount=10,filesize=4M -jar GCExample.jar `` +Generate the archive classes. -##Running with JFR options (JDK 11): -``java -Xms50m -Xmx50m -XX:+FlightRecorder -XX:StartFlightRecording=disk=true,maxage=2h -jar GCExample.jar `` +`` sudo java -Xshare:dump -jar CDSExample.jar `` -##Taking the dump: -``jcmd 25270 JFR.dump name=1 filename=`pwd`/gc.jfr`` +Should produce the archive file called classes.jsa at: +`` ls -lh /Library/Java/JavaVirtualMachines/yjava_jdk-8.0_8u242b08.3911256/Contents/Home/jre/lib/server/classes.jsa `` -##Open JFR file with Java Mission Control and analyze. -Running GCViewer to analyze gc logs: +Use the share file to run: -``java -jar gcviewer-1.36.jar`` +``java -Xshare:on -jar CDSExample.jar `` + +##Running with CDS options (JDK 11): + +create a list of classes to include in the archive -XX:DumpLoadedClassList + +``java -XX:DumpLoadedClassList=classes.lst -jar CDSExample.jar`` + + +Generate the archive classes. + + +`` sudo java -Xshare:dump -jar CDSExample.jar `` + +Use the share file to run: + +``java -Xshare:on -jar CDSExample.jar `` + + +##Running with CDS options (JDK 12): + +The JDK comes with one and uses it automatically. If you want to turn that off, launch your application with -Xshare:off. + + + +##Launch Time Measurements + +On: + +``time java -Xshare:on -jar CDSExample.jar `` + +``` +amountblood-lm:java-gc-example charlesk$ time java -Xshare:on -jar CDSExample.jar +Starting CDSExample... +args: [] +Finished CDSExample. + +real 0m0.070s +user 0m0.049s +sys 0m0.022s +``` + + +Off: + +``time java -Xshare:off -jar CDSExample.jar `` + +``` +amountblood-lm:java-gc-example charlesk$ time java -Xshare:off -jar CDSExample.jar +Starting CDSExample... +args: [] +Finished CDSExample. + +real 0m0.088s +user 0m0.065s +sys 0m0.025s +``` + + + +The interesting bit is real, which gives the wall-clock time it took to run the app. 70 ms to 88ms (without class data sharing) + + + + + +## NOTES: + +On Java 12+, Java classes are automatically loaded from the archive included in the JDK + +On Java 13+, class-data archives can be automatically created by the JVM on shutdown with the command line option -XX:ArchiveClassesAtExit=${ARCHIVE}; to use it, add -XX:SharedArchiveFile=${ARCHIVE} on launch + +On Java 10+, it is possible to hand-craft archives in three steps: + +create a list of classes to include in the archive +$ java + -XX:DumpLoadedClassList=classes.lst + -jar app.jar + +create the archive +$ java + -Xshare:dump + -XX:SharedClassListFile=classes.lst + -XX:SharedArchiveFile=app-cds.jsa + --class-path app.jar + +use the archive +$ java + -XX:SharedArchiveFile=app-cds.jsa + -jar app.jar + + \ No newline at end of file diff --git a/gcviewer-1.36.jar b/gcviewer-1.36.jar deleted file mode 100644 index 42d611f..0000000 Binary files a/gcviewer-1.36.jar and /dev/null differ diff --git a/src/com/cds/example/CDSExample.java b/src/com/cds/example/CDSExample.java new file mode 100644 index 0000000..7bc0a3e --- /dev/null +++ b/src/com/cds/example/CDSExample.java @@ -0,0 +1,18 @@ +package com.cds.example; + +import java.util.Arrays; + +public class CDSExample { + + public static void main(String[] args) { + + System.out.println("Starting CDSExample..."); + + System.out.println("args: " + Arrays.toString(args)); + + System.out.println("Finished CDSExample."); + + } + + +} diff --git a/src/com/cds/example/manifest.txt b/src/com/cds/example/manifest.txt new file mode 100644 index 0000000..d6c67b3 --- /dev/null +++ b/src/com/cds/example/manifest.txt @@ -0,0 +1 @@ +Main-Class: com.cds.example.CDSExample diff --git a/src/com/gc/example/GCExample.java b/src/com/gc/example/GCExample.java deleted file mode 100644 index 9f9c843..0000000 --- a/src/com/gc/example/GCExample.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.gc.example; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class GCExample { - - public static void main(String[] args) { - - System.out.println("Starting GCExample..."); - - System.out.println("args: " + Arrays.toString(args)); - - causeOOM(); - - while (true) { - - try { - - System.out.println("Sleeping..."); - Thread.sleep(5000); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - } - - private static void causeOOM() { - - List items = new ArrayList<>(1); - try { - while (true) { - items.add(new Object()); - } - } catch (OutOfMemoryError e) { - System.err.println(e); - } - - } - -} diff --git a/src/com/gc/example/manifest.txt b/src/com/gc/example/manifest.txt deleted file mode 100644 index c0e77fb..0000000 --- a/src/com/gc/example/manifest.txt +++ /dev/null @@ -1 +0,0 @@ -Main-Class: com.gc.example.GCExample