This Just-In-Time Compiler translates the java bytecode into native code. The compilation doesn't happen at class-level but at method-level. It doesn't occur the first time the method is invoked because it will lead to a compilation of many methods by decreasing the speed of execution instead of incresing performance; That's why the JIT happens when a certain number of calls are made to a method. How much?, Well this is a configurable option for the JVM but in HotSpot Client JVM the default is 1,500.
Let's see an example:
(Based in a JavaWorld article, See references)
public class JIT{
public static void main (String [] args)
{
for (int repeat = 0; repeat < 25; ++ repeat){
System.out.println("Repeat: " + repeat);
sum (100)
}
}
public static int sum (int n){
if (n <= 1)
return 1
else
return n + sum (n - 1);
}
}
To Run this example we will use:> java -XX:+PrintCompilation JIT
This Options will print when the JIT compiler comes in action, and what method it compile.
The Ouput was:
1 java.lang.String::hashCode (60 bytes)
2 java.lang.String::charAt (33 bytes)
Repeat: 0
Repeat: 1
Repeat: 2
Repeat: 3
Repeat: 4
Repeat: 5
Repeat: 6
Repeat: 7
Repeat: 8
Repeat: 9
Repeat: 10
Repeat: 11
Repeat: 12
Repeat: 13
Repeat: 14
3 Repeat: 15 JIT::sum (16 bytes)
Repeat: 16
Repeat: 17
Repeat: 18
Repeat: 19
Repeat: 20 4 java.lang.String::indexOf (151 bytes)
Repeat: 21
Repeat: 22
Repeat: 23
Repeat: 24
The compilation happens in the Repeat 15 because at this time, the Method was executed 1,500 times and will execute faster. There's a way to change the times the method need to be called:
-XX:CompileThreshold=[Number]
With this we can have a little control over JIT, but do not try to reduce this number too much, because the startup of the application can be more slow because of the compile action. Remember that this affects not only your classees but all java classes.
References:
Watch yout HotSpot Compiler Go.
http://www.javaworld.com/javaworld/javaqa/2003-04/01-qa-0411-hotspot.html
Wilson, S. Kesselman, F. Java Platform Perfomance Strategies and Tactics. Addison-Wesley 2000.
0 comments:
Post a Comment