We already studied about Java Virtual Machine (JVM) in detail and observed that the diagram in the previous post was showing a part inside the JVM called JIT. So in this post we will learn about Just-In-Time(JIT) compiler , the component of Java Runtime Environment. You must be having a question in mind "Why does this JIT thing is residing inside the JVM ?"
Answer is : -
Just-In-Time is nothing but a component of JDK that improves the
performance of Java applications at run time which in turn speeds up the
execution time.
JIT : -
- The JIT compiler was first made available as a performance update in the JavaDevelopment Kit (JDK) 1.1.6 .
- Sun Microsystems
suggests that it's usually faster to select the JIT compiler option,
especially if the method executable is repeatedly reused.
- According
to most researches, 80% of execution time is spent in executing 20% of
code. That would be great if there was a way to determine those 20% of
code and to optimize them.
That's exactly what JIT does - during runtime it gathers statistics, finds
the "hot" code compiles it from JVM interpreted
bytecode (that is stored in .class files) to a native code that is
executed directly by Operating System and heavily optimizes it.
- The name
"Hotspot" of Sun (Oracle) JVM is
chosen because of the ability of this Virtual Machine to find
"hot" spots in code.
- A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster, typically the host CPU's native instruction set.
- It tries to predict which instructions
will be executed next so that it can compile the code in advance.
- Compiled code resides in memory
until the application is closed.
Working of JIT
- Just-In-Time compiler basically performs the following steps :-
1. Reads all the byte codes
for the method that needs to be executed
2. Converts all those byte codes to native machine instructions
3. Executes the generated native machine instructions
After the conversion of byte codes for a method to native machine
instructions , that native code is remembered by the JVM .This enables JVM to
simply run the native instructions , when next time the method has to be run.
It doesn't need to convert
the bytecodes every time the method is run. This makes the program run much
faster.
Note:- The JIT compiler can be disabled, in which case
the entire Java program will be interpreted. Disabling the JIT compiler is not
recommended except to diagnose or work around JIT compilation problems.
So we have learnt the secret behind rapid
execution of Java Programs which is Just-In-Time (JIT) compiler.
Keep Compiling !!
Just In Time Compiler
Reviewed by shashank
on
10:48
Rating:
No comments: