In this post, I am going to explain you about Just-In-Time Compiler. Many developers either have no clear understanding or have partial understanding of JIT. Some developers have misconception as well. I will try to explain the logic behind JIT and also try to clear the misconceptions regarding JIT.

 

WHY DOES JIT EXIST ?

Developers need to know the reason behind the existence of JIT. Why does it exist ? What problem does it solve ? The answer lies in the time of execution and the fact that Java is platform independent. Platform dependent languages like C,C++ compile the code into machine language directly. This makes the execution of the program faster as the CPU can directly execute the machine language. But  the point here to note is that the execution is faster because the code was directly compiled to machine language and this compilation of code to machine language requires that the compilation to be done in the same environment where its going to run. So in short, faster execution is achieved on the price of platform dependence. Now coming to Java, first thing is that Java is platform independent, meaning the code in not (or rather can’t be) compiled into machine language. The code is actually compiled into byte code, the .class files that you get after running javac command. This byte code is platform independent and hence can be used in any environment once its created. But  this byte code has to be converted to machine language eventually, right ? When does this happen ? This happens during the execution of the program when you run java command. This is done inside or by the JVM before the CPU executes the machine language. Now there could be two easy ways to achieve this:

  1. As soon as the java command is run, the JVM converts all the bytecode into machine language once before giving it to the CPU. This would take an extra start-up time, but once this is done the execution later on would be normal.
  2. The JVM interprets the bytecode one-by-one (instead of converting all at once), converts the byte code to machine language and gives it to the CPU one-by-one. This would not require any start-up time, but the execution later on would be slower.

 

I guess, the earlier versions of Java used the 2nd approach and that’s why Java was criticized to be a bit slower by few developers. Just-In-Time (JIT) compiler tries to solve this problem by using a mixed approach.

 

HOW AND WHAT DOES JIT DO ?

Just-In-Time compiler runs or comes into picture only after the code is compiled to bytecode. Many developers have this confusion that the JIT runs during the compilation of java code (on execution of javac command) to bytecode, which is not true. Just-In-Time compiler is a component or feature of JVM. It only runs during the execution of the program, i.e. on execution of java command. JIT tries to reduce the overall execution time of the program or in simple words makes the program run faster. How does it do this ? What JIT does, is that it converts the byte codes into machine language only for a few methods or section of the byte code, caches it and gives it to the CPU as required. So because the conversion of bytecode into machine language is done only for specific methods or section of code, the startup time is reduced and because the machine language is cached for those specific methods or section of code, the execution time later on is also maintained. The logic for choosing those specific method or section of code is also done at run time. It maintains a count of execution for all methods or section of code, if the count reaches a threshold, it chooses that method for the conversion and caching. In fact, JIT starts with the 2nd approach (discussed above) keeping the counters set to 0. If at any time, the counter reaches the threshold for any method or section of code, it converts the bytecode into machine language (1st approach) and caches it for later use. So basically JIT uses the 1st approach only for most used methods or section of code. It is possible that for small stand-alone programs the counter never reaches the threshold for any method or section of code.


In most of the cases JIT does come into picture and reduces the overall execution time and makes the program run faster. The clear understanding of JIT is important for any Java developer during regular work or during interviews.