Tuesday, February 13, 2007

When JIT happens ?

Since Java works much like a interpreted language, it is argued that Java program will run a lot slower then their native language counter parts. To avoid this Sun came up with Just in Time compiler, which will change the Java byte code to native machine dependent assembly. This will make Java run as fast as an Application created using 'C'.

This JIT will not happen always. There is criteria at which this will happen. The following things discusses about when JIT will happen and how it can be configured during JVM invocation.

The following link provides much more details about all the command line options available.
http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp

Compiler Option: -XX:CompileThreshold=10000
Purpose : Number of method invocations/branches before compiling [-client: 1,500]

This options specifies that when a particular method is invoked 10000 times, then compile this method to native code.

INorder to know when the native compilation has happened we need to use the following JVM command line options.
java -XX:+PrintCompilation

The following link provides much more interesting test results. I enjoyed reading it.
http://www.javaworld.com/javaworld/javaqa/2003-04/01-qa-0411-hotspot.html

So If we could run Java Application at native code speed then why not just do it always. As specified in the above location, the problem could be.....

As an experiment, you might consider shortening the warm-up period by manipulating the -XX:CompileThreshold JVM option, although you will soon discover that making this threshold too small will delay your application's startup, as HotSpot begins compiling just about every method it discovers into native code, including methods in the core Java libraries.

So, it is advised to set the CompileThreshold option to 10000 for Server applications and 1500 for Client application.

No comments: