What is JIT compiler?

The Just-In-Time (JIT) compiler is a component of the JRE (Java Runtime Environment) that improves the performance of Java applications at run time. It helps improve the performance of Java programs by compiling bytecodes into native machine code at runtime .

How JIT compiler works

Java programs using a compiler (javac) for converting Java source code(.java files) to Java bytecode (.class files). Once this is done, Java Virtual Machine (JVM) loads the .class files at run time and converts them to a machine understandable code using an interpreter. Just-In-Time (JIT) compiler is a feature of JVM which when enabled makes the JVM analyse the method calls in byte code and compiles them to more native and efficient code. At this point of time, Just-In-Time (JIT) optimizes the prioritized method calls. Once these method calls are compiled, the JVM then executes this optimized code instead of interpreting it. Ideally the efficiency of running object code will overcome the inefficiency of recompiling the program every time it runs. In practice, methods are not compiled the first time they are called. For each method, the Java Virtual Machine maintains a call count, which is incremented every time the method is called. The JVM interprets a method until its call count exceeds a Just-In-Time compilation threshold. Therefore, often-used methods are compiled soon after the Java Virtual Machine has started, and less-used methods are compiled much later, or not at all. The Just-In-Time 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.

Advantages of Just-In-Time (JIT) compiler

Improvements in compiler technology can have an impact on existing programs. For ex. A better "C" compiler does not help you at all with programs already deployed earlier. A better JIT-compiler will improve the performance of existing programs. So, the Java code you wrote years back will run faster today.

Native images load faster because they don't have much start-up activities, and require a static amount of fewer memory

Disadvantages of Just-In-Time (JIT) compiler

Just-In-Time (JIT) compiler increases level of unpredictability and complexity in Java program. It adds another layer that programmers don't really understand or control. JIT compilers have a lot more memory overhead since they need to load a compiler and interpreter in addition to the runtime libraries and compiled code that an ahead-of-time compiled program requires. Large applications generally benefit from being compiled ahead-of-time (AOT), and small ones generally don't.