Search This Blog

Loading...

Wednesday, February 20, 2013

Difference between JIT and JVM in Java - Interview Question

Main difference between JIT and JVM is that, JIT is part of JVM itself and its main function is to improve performance of JVM by directly compiling some hot code (code which executes above 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 platform to execute or run Java programs. Main advantage of JVM is that, JVM  makes Java platform independent by executing byte codes. Java source code is compiled into class files, which contains byte code. These byte codes are then executed by JVM. Now here comes JIT. Since execution of byte code is slower than execution of machine language code, because JVM first needs to translate byte code 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 result in improved performance of JVM. by the way difference between JVM and JIT is also a good Java interview question to ask. Well, this is just a simple explanation, JIT is lot more complex than this. There are sophisticated algorithm which helps JIT to pick most executed code for compiling into machine code. Earlier we have seen difference between JRE and JDK and in this post we will understand difference between JVM and JIT. Let's see next section for more difference between JIT vs JVM.



JVM vs JIT

Just in time compiler or JIT is an integral component of Java Virtual Machine along with Garbage collector, which as name suggest does Just in time compilation. It is capable of compiling Java code directly into machine language, which can greatly improve performance of Java application. By the way, its not guaranteed that which code will be compiled and when. JIT usually compile hot code in Hotspot JVM once its execution cross certain limit e.g. a method will be converted into machine language if it is called more than 10K times etc.

Difference between JVM and JIT in Java


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

1) Main difference between JVM and JIT is there purpose, main goal of JVM is to provide platform independence while objective of JIT is to improve 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 compile 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 hotpost because its Just in time compiler compiles only hot code into native language, code which execute 90% of time. There is threshold setup, if some code executes more than that threshold then it become eligible for just in time compiled. By the way, Hotspot is not the only JVM which contains Just in time compilers, there are others JVM as well e.g. Oracle's original JRockit one. 

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

JIT vs JVM in Java

That's all on 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.


Other Java programming articles from Java67

3 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