How does JIT (just in time) compiler works

JIT compilation does require processor time and memory usage. When the JVM first starts up,thousands of methods are called. Compiling all of these methods can significantly affect startup time,even if the program eventually achieves very good peak performance.In practice, methods are not compiled the first time they are called. For each method, the JVM maintains a call count, which is incremented every time the method is called. The JVM interprets a method until its call count exceeds a JIT compilation threshold. Therefore, often-used methods are compiled soon after the JVM has started, and less-used methods are compiled much later, or not at all. The JIT compilation threshold helps the JVM start quickly and still have improved performance. The threshold has been carefully selected to obtain an optimal balance between start up times and long term performance

After a method is compiled, its call count is reset to zero and subsequent calls to the method continue to increment its count. When the call count of a method reaches a JIT recompilation threshold, the JIT compiler compiles it a second time, applying a larger selection of optimizations than on the previous compilation. This process is repeated until the maximum optimization level is reached. The busiest methods of a Java program are always optimized most aggressively, maximizing the performance benefits of using the JIT compiler.The JIT compiler can be disabled, in which case the entire Java program will be interpreted. Disabling the JIT compiler is not recommended except to diagnose or work around JIT compilation problems.

1. Inlining – One of the most common JIT compilation techniques used by Java HotSpot VM is inlining, which is the practice of substituting the body of a method into the places where that method is called. Inlining saves the cost of calling the method; no new stack frames need to be created. By default, Java HotSpot VM will try to inline methods that contain less than 35 bytes of JVM bytecode.

2. Native code generation – Native code generation processes vary, depending on the platform architecture.Trees of a method are translated into machine code instructions

3. Local optimizations – analyze and improve a small section of the code at a time

4. Control flow optimizations – Finally it can do some great tricks by compiling while it’s running. with a method like this:

public void testFunc(int param) {
    if(param == 25) 
        do bunches of crap;
    else if (param > 5)
        do much more crap;

The compiler can call it once or twice and on the fly recognize that values 5 and under just return. If this is called all the time with a value of 2 it can replace the ENTIRE method call with if (param != 2) testFunc(param); which eliminates the entire method call for that number.

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *