Execution of a Java Program & Role of Just In Time (JIT) Complier

  • When we write a Java program and issue a javac command , the program is compiled to bytecode.
  • To execute this program, we issue a java command and when we do so, the bytecode generated by javac is interpreted by the Java Virtual Machine (JVM) to produce machine code which is ultimately fed to CPU to execute it.
  • This approach of compiling the java code to bytecode first is what brings the portability or write-once-run-anywhere feature of java but to execute the code on a particular operating system the bytecode has to go through the interpreter.
  • Interpreter goes through one statement at a time and this slows down the performance of Java application
  • To maximize performance, Java utilizes a just-in-time (JIT) compiler to dynamically compile bytecode to machine code. This JIT compiler is baked into the JVM and works automatically without any developer intervention.
  • Before going into the details of JIT , let us understand the difference between a compiler and an interpreter
  • Compiled languages like C, C++ compile source code into an executable binary file that runs directly on the CPU. They are fast but lack portability
  • Interpreted languages read source code line by line, generating the binary instructions through the interpreter itself. This makes it possible for interpreted languages to be “platform agnostic” meaning they can run on different operating systems in the same way.
  • Due to the performance of interpreter , Java has a JIT compiler which allows it to be both a compiled and interpreted language.
  • JIT compiler is a part of JVM and does not need any additional configuration. It continuously runs to optimize performance.
  • While the JVM interprets bytecode, the JIT analyses execution and dynamically compiles frequently executed bytecode to machine code. This prevents the JVM from having to interpret the same bytecode over and over.
  • JIT tracks the most frequently called methods and dynamically compiles these to machine code at runtime.
  • Instead of interpreting the statement line by line by leveraging JIT the most frequently accessed piece of code can be compiled and cached into memory for execution
  • While the JVM initially interprets code, the JIT compiler works to selectively compile frequently executed code “just in time”.
  • The Java Virtual Machine from Oracle is called Java HotSpot VM . In a program only certain sections of the code is executed frequently and the performance of an application depends primarily on how fast those sections of code are executed. These critical sections are known as the hot spots of the application; the more the section of code is executed, the hotter that section is said to be. Hence the name Java HotSpot VM

Thank You !!!

One thought on “Execution of a Java Program & Role of Just In Time (JIT) Complier

Leave a Reply