Bright loading of java class

I am trying to benchmark a JVM running on various hardware and OS platforms. I have created an algorithm to implement the parts of the JVM that interest me, and I intend to run this algorithm repeatedly to find a decent average.

When I run the test, I find that the first run is much longer than subsequent runs:

132ms 86ms 77ms 89ms 72ms 

My suspicion is that classes load lazily, putting a lot of overhead on first run. Although this is indeed a feature that I believe is unique to every JVM, I'm not interested in that.

Is there a standard command line option or property for actively loading classes? or does anyone have other theories?

+6
java
source share
6 answers

The simplest task is to ignore the first run. (If this is a valid thing) Note: if you run the same code 10,000 times, it will compile the code further and you will get better results, so you can ignore the first 10K results for some micro tests.

Some JVMs support booting, but I don't think that the Sun JVM does.

+5
source share

Rule number 1 for Java benchmarking: the first 15,000 times (or so) the method does not work interestingly.

This thread contains solid tips: How to write the right micro-test in Java?

+4
source share

If you want to force classes to load, do the following:

 public class Main { static { loadClasses(); } public static void main(final String[] argv) { // whatever } private static void loadClasses() { final String[] classesToLoad; // even better, read them from a file and pass the filename to this method classesToLoad = new String[] { "foo.bar.X", "foo.bar.Y", } for(final String className : classesToLoad) { try { // load the class Class.forName(className); } catch(final ClassNotFoundException ex) { // do something that makes sense here ex.printStackTrace(); } } } } 
+4
source share

Use java -XX:+TraceClassLoading to track the loading of classes.

Use java -XX:+PrintCompilation to track when methods are JITed.

+2
source share

Once your classes are loaded to avoid using the interpreter, use -Xcomp (when implementing Sun) to run only compiled code. It can be very slow to run regular applications this way, since all the code needs to be compiled, not just a few parts.

+1
source share

There is no standard command line option, as this is not part of the JVM specification. Loading the Lazy class (explicitly permitted as) is part of the JVM specification. One could use Class.forName() before starting to load classes that you know about, but this is not automatically transient.

HotSpot compilation also affects the first few starts - the method is interpreted several times before compilation, and the compilation process takes some time.

0
source share

All Articles