Difference between JIT and JVM in Java? Answered

The main difference between JIT and JVM is that JIT is part of JVM itself and its main function is to improve the performance of JVM by directly compiling some hot code (code that executes above a certain threshold) into native instruction. JIT stands for Just In time compilation and JVM stands for Java Virtual Machine. JVM is a virtual machine used in Java programming platforms to execute or run Java programs. The main advantage of JVM is that JVM  makes Java platform-independent by executing bytecodes. Java source code is compiled into class files, which contain bytecode.

These byte codes are then executed by JVM. Now here comes JIT. Since the execution of bytecode is slower than the execution of machine language code because JVM first needs to translate bytecode into machine language code.

JIT helps JVM here by compiling currently executing byte code into machine language. JIT also offers caching of compiled code which results in improved performance of JVM.

By the way, the difference between JVM and JIT is also a good Java interview question to ask. Well, this is just a simple explanation, JIT is a lot more complex than this. There is a sophisticated algorithm that helps JIT to pick the most executed code for compiling into machine code.

Earlier we have seen the difference between JRE and JDK and in this post, we will understand the difference between JVM and JIT. Let's see the next section for more differences between JIT vs JVM.



What is difference between JVM vs JIT in Java?

While both JVM and JIT are part of Java platform but key difference between them is that JVM is an interpreter while JIT is a compiler.  JVM is mandatory to run a Java program as it convert Java byte code to machine code. JIT comes next to improver performance by replacing hot Java byte code with machine code.  

Just in time compiler or JIT is an integral component of Java Virtual Machine along with Garbage collector, which as the name suggests does Just in time compilation. It is capable of compiling Java code directly into machine language, which can greatly improve the performance of Java applications.

By the way, it's not guaranteed that which code will be compiled and when. JIT usually compiles hot code in Hotspot JVM once its execution cross a certain limit like a method will be converted into machine language if it is called more than 10K times etc.

JIT vs JVM in Java






Difference between JIT and JVM in Java?

Here are a couple of more differences between JVM and JIT in the Java programming platform :

1. The main difference between JVM and JIT is their purpose, the main goal of JVM is to provide platform independence while the objective of JIT is to improve the performance of JVM, by compiling more code into machine language. 

Just keep in mind that this compilation also takes time, so translating all code into native code is not worth doing. That's why JIT mostly compiles frequently used code into native code.

2. Another difference between JIT and JVM is that JIT is part of JVM. One example of JIT is Oracle's Hotspot JIT which comes with Hotspot JVM. It is called hotspot because it's Just in time compiler compiles only hot code into the native language, code which executes 90% of the time.

There is a threshold set up, if some code executes more than that threshold then it becomes eligible for just in time compiled. By the way, Hotspot is not the only JVM that contains Just in time compilers, there are other JVM as well like Oracle's original JRockit one. 

3. At last, JVM is an older concept than JIT. JIT actually invented to improve the performance of JVM after years of the initial release of Java, while Java virtual machine was part of the initial Java release. 

4. Also there are multiple compiler level in JIT like C1, C2 which defines level of how aggressive compilation you want. There are also JVM settings which you can use to enable and disable these compiler for your application. 

Difference between JVM and JIT in Java

That's all on the difference between JVM and JIT in Java. As I said, JIT is part of JVM and used to improve JVM performance by dynamically compiling or translating Java byte codes into native machine language code during execution time. JVM is essential to run your Java program as it convert Java byte code to machine code and Java program runs inside JVM. It's also one of the reason why Java is platform independent. 


Other Java programming articles from Java67

Thanks for reading this article so far. If you found this JVM vs JIT question interesting and my answer and explanation then please share with your friends and colleagues. If you have any question or feedback then please drop a note. 


P. S.  - If you are serious about mastering JVM and Java performance in-depth then you can also check out this Udemy best-selling course - Java Application Performance and Memory Management course by Matt Greencroft. It's a great course for experienced Java developers.

4 comments:

  1. If you are curious to see What kind of optimization or compiling JIT is doing, you can switch it on by using JVM option -XX:+PrintCompilation

    ReplyDelete
  2. I want to know ----Every JVM consists JIT.

    ReplyDelete
  3. How does the JVM knows which part of the code executes more frequently without actually executing it?

    ReplyDelete
  4. It is done through profiler, a separate module that keeps track of the frequency of code usage.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.