What is JIT (Just-In-Time) Compiler in Java? Explain JIT Compilation.

The Just-In-Time (JIT) compiler is a component of the Java Runtime Environment (JRE). It improves the performance of Java applications by compiling bytecodes to native machine code at run time. When a Java program is run JVM launched that interprets the byte code and provides result. At run time, the JVM loads the class files, determines the semantics of each individual bytecode, and performs the appropriate computation. In this process, the JIT compiler helps improve the performance of Java programs by compiling bytecodes into native machine code at run time.

The JIT compiler is enabled by default, and is activated when a Java method is called. The JIT compiler compiles the bytecodes of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it. Theoretically, if compilation did not require processor time and memory usage, compiling every method could allow the speed of the Java program to approach that of a native application.

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 startup 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 also measure operational data at run time, and use that data to improve the quality of further recompilations.

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. Following figure demonstrates the JIT compile process pictorially.

JIT Compile Process
Fig 1: JIT Compile Process (Courtesy: Oracle documentation)

Hope you have enjoyed reading about just-in-time (JIT) compiler. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

WannaCry ransom notice analysis suggests Chinese linkPosted on Monday May 29, 2017

Researchers say the WannaCry ransom note was poorly translated - possibly using Google Translate.

BA boss 'won't resign' over flight chaosPosted on Monday May 29, 2017

Chief executive Alex Cruz says flight disruption at Heathrow and Gatwick had nothing to do with cost cutting.

Hay Festival 2017: Stephen Fry's warning for the webPosted on Sunday May 28, 2017

Delivering a lecture at the Hay Festival, the actor said society could face "dire consequences".

Courtesy BBC News