Core Java Tutorial 08 – Just in Time Compiler ( Understanding JVM Working in detail )
In this video tutorial we will learn the concept of Just in time compiler (or, just in time compilation approach). This tutorial will cover the working of a JVM in detail and the two approaches followed by Java i.e interpretation and Just in time compilation ( JIT)
For our readers who are unable to access the Video Tutorial!!!
Dear readers ; so far we have learnt how Java converts the source program into its bytecode form and then, JVM on every computer executes this bytecode. For executing a bytecode on any computer, JVM converts bytecode into the machine language and once the program is into the machine language form, the computer executes it.
In this tutorial we will discuss the working of a JVM ( in little more depth ) on any computer. In the initial years of Java programming development, Java people used a Straight Forward Approach for converting bytecode into the machine language and then running it. This approach was very simple in which the bytecode was converted and executed line by line into the machine language i.e JVM converts the first line of bytecode into the machine language and computer runs the first line and then JVM converts the second line of bytecode into the machine language and computer runs this second line- in this way the process continues till the complete bytecode is converted by the computer.This way of converting and running a program line by line is called INTERPRETATION. This approach is a good but again not the best as there is a serious performance problem with this approach: i.e much time is needed to run a bytecode on any computer.
So Java people made a little change in this approach so that the same bytecode can run faster on any computer by a JVM-This new approach was called JUST in Time Compilation Approach ( JIT ) and was based on two important observations:
The first observation was- in general in any Java program or a application there are some portions of code which are actually critical– meaning computer keeps on executing these portions of code repeatedly for most of its execution time. So why not convert the bytecode for all these critical portions just once and use this already converted code next time when computer executes the same portion of bytecode.
If a line in the bytecode is going to be executed by a computer say for 1000 times, then in the earlier approach ( Interpretation Approach ), JVM would 1000 times need to convert this bytecode line into the machine language form whereas JIT Approach says – if a JVM knows that 1000 times this line is going to be executed by the computer then why not convert this line into the machine language just once and save the converted line into computer’s memory and all other 999 times just use this already converted line, and in this way, JVM will save a lot of time in the JIT Approach which it was used to waste in the first approach.
This is an important point which if a JVM considers then definitely performance of running a bytecode on any computer will be improved greatly.
One more observation made by the Java people was that programmers may make silly mistakes while writing the program which may hamper the speed with which the computer runs the program.So Why not before converting and running a program – the JVM should optimize the program ??? ( that means correct all the silly mistakes and then convert the program into the machine language and then run it).
But again it is not always because of a programmer’s mistake a JVM needs to optimize the program but also due to the fact that different computers executes a program using different ways on them ( different computers may have different combination of a microprocessor and an operating system on them ), it is really needed to optimize the program for a particular computer.
A JVM program these days use a approach based on these two observations and as a result runs a bytecode faster on any computer than a JVM based on the first approach. This new approach based on these two observations points is known as Just In Time Compilation Approach.
There are different companies in market which develop JVMs for computers. Some companies’s JVM use a kind of mix approach others use only the second approach for running a bytecode on any computer. All companies follow different strategies of finding which portions of a program are critical, which portions of a program needs to be optimized and which portions need to be run line by line.