There is no reason why a java programmer should not know how JIT works inside JVM.
Below picture just shows main components of a JVM
Main Components of JVM are Class Loader, Bytecode Verifier, Interpreter and Garbage Collector. I will take up only Interpreter in this post.
Interpreter - Interpreter come with a sub-component called JIT Compiler. Main task of the JIT is to improve runtime performance of a java program by compiling bytecode to machine code and caching the resultant machine code(This compilation should not be confused with javac compilation).
So when JIT is ON, it is JIT COMPILATION + INTERPRETATION
When JIT is OFF, it is only INTERPRETATION
Let us take IBM jvm J9 and try to understand how JIT works.
When jvm is started, it might have lot of methods. JIT will not compile all the methods.First few calls to all methods are infact interpreted.It maintains a counter on each method call. When this counter crosses some threshold it will be JIT compiled and cached. And all the future calls to that method are not interpreted, in fact it is taken from JIT cache. Once the counter reaches the threshold it is reset to 0. when the threshold reaches second time, it is compiled with more optimization and cached . so more the times a method is called it is more optimized.The counter threshold value is chosen such that neither there is start up delay nor degraded performance.
JIT improves performance by optimization techniques like inlining, control flow optimization,local/global optimizations.
Client option in the JVM does less JIT compilations and hence less optimizations, while Server option does more JIT JIT compilations and optimizations. Hence there will be more starup delay with Server option.
Below is a small program which depicts the performance benefit we get when JIT compiled
Below is a small program which depicts the performance benefit we get when JIT compiled
By default, JIT will be ON. to switch off JIT(which is not advisable) option used is -Djava.compiler=none
With JIT
when we run above program with JIT enabled, it takes 1-2 milliseconds as seen below.
Without JIT
when we run above program with JIT disabled, it takes 8-9 milliseconds as seen below.
3 comments:
Awesome and instructive post Ashwin. I did not know about the JIT compiler. Very informative. Thanks for sharing this :)
Really nice post.Thanks for sharing this.After reading this I am getting my interest back in Hotspot JVM. Expectig few more posts from you on hotspot features.
@sharath - thanks maga :)
@kulbhushan - thanks buddy :) itz my pleasure you liked the post..surely share more info about jvm as and when i get to know more :)
Post a Comment