JIT Compiler (Just In Time Compiler)
JIT compiler is a part of the JVM.
JIT compiles bytecodes to machine code at run time and improves the performance of Java applications.
JIT Compiler internal working >
JIT compilation does require processor time and memory usage. When the JVM first starts up, lots of methods are called. Compiling all of these methods might can affect startup time significantly, though program ultimately may achieve good performance.
Methods are not compiled when they are called first time. For each and every method JVM maintains a call count, which is incremented every time the method is called. The methods are interpreted by JVM until call count not exceeds JIT compilation threshold (The JIT compilation threshold improves performance and helps the JVM to start quickly. The threshold has been selected carefully by java developers to obtain an optimal performances. Balance between startup times and long term performance is maintained).
Therefore, very frequently used methods are compiled as soon as JVM has started, and less used methods are compiled later.
How JIT improves performance of Most frequently used methods ?
After a method is compiled, its call count is reset to zero and subsequent calls to the method increment it call count. When the call count of a method reaches a JIT recompilation threshold, the JIT compiler compiles method second time, applying more optimizations as compared to optimizations applied in previous compilation. This process is repeated until the maximum optimization level is reached. Most frequently used methods are always optimized to maximize the performance benefits of using the JIT compiler.
Example -
Let’s say JIT recompilation threshold = 2
After a method is compiled, its call count is reset to zero and subsequent calls to the method increment it call count. When the call count of a method reaches a 2 (i.e. JIT recompilation threshold), the JIT compiler compiles method second time, applying more optimizations as compared to optimizations applied in previous compilation.
Disabling JIT compiler >
The JIT compiler can also be disabled, if disabled the entire Java program will be interpreted. Disabling the JIT compiler is not recommended. We must disable JIT to diagnose JIT compilation problems only.
Labels:
Core Java
core java Basics