Wednesday, October 19, 2011

Understanding JIT(Just Intime Compiler) in JVM

There is no reason why a java programmer should not know how JIT works inside JVM.
Below picture just shows main components of a JVM

Main Components of JVM are Class Loader, Bytecode Verifier, Interpreter and Garbage Collector. I will take up only Interpreter in this post.
Interpreter - Interpreter come with a sub-component called JIT Compiler. Main task of the JIT is to improve runtime performance of a java program by compiling bytecode to machine code and caching the resultant machine code(This compilation should not be confused with javac compilation).

So when JIT is ON,  it is JIT COMPILATION + INTERPRETATION
When JIT is OFF, it is only INTERPRETATION

Let us take IBM jvm J9 and try to understand how JIT works.
When jvm is started, it might have lot of methods. JIT will not compile all the methods.First few calls to all methods are infact interpreted.It maintains a counter on each method call. When this counter crosses some threshold it will be JIT compiled and cached. And all the future calls to that method are not interpreted, in fact it is taken from JIT cache. Once the counter reaches the threshold it is reset to 0. when the threshold reaches second time, it is compiled with more optimization and cached . so more the times a method is called it is more optimized.The counter threshold value is chosen such that neither there is start up delay nor degraded performance.

JIT improves performance by optimization techniques like inlining, control flow optimization,local/global optimizations.

Client option in the JVM does less JIT compilations and hence less optimizations, while Server option does more JIT JIT compilations and optimizations. Hence there will be more starup delay with Server option.
Below is a small program which depicts the performance benefit we get when JIT compiled



By default, JIT will be ON. to switch off JIT(which is not advisable) option used is -Djava.compiler=none

With JIT
when we run above program with JIT enabled, it takes 1-2 milliseconds as seen below.


Without JIT
when we run above program with JIT disabled,  it takes 8-9 milliseconds as seen below.

Friday, March 11, 2011

My favorite quotes related to software,programming

In order to understand recursion, one must first understand recursion - Author Unknown

Before software can be reusable it first has to be usable. - Ralph Johnson

A good programmer is someone who always looks both ways before crossing a one-way street - Doug Linder

Programmers always confuse Halloween with Christmas because OCT 31 = DEC 25 - Author Unknown

There are two ways to write error-free programs; only the third one works. - Alan J. Perlis

Real programmers don't comment their code. It was hard to write, it should be hard to understand - Author Unknown

I would like to change the world But I do not have source code - Author Unknown

Low-level programming is good for the programmer’s soul.- John Carmack

"Kevorkian Virus: helps your computer shut down whenever it wants to." - Author Unknown

"Cannot delete tmp150---3.tmp: There is not enough free disk space. Delete one or more files to free disk space, and then try again." - Author Unknown

I think Microsoft named .Net so it wouldn’t show up in a Unix directory listing.- Oktal

If builders built buildings the way programmers wrote programs, then the first woodpecker that came along wound destroy civilization. - Gerald Weinberg

My programs never have bugs, they just develop random features - Author Unknown

Programs for sale: fast, reliable, cheap - choose two. - Author Unknown

You cannot teach beginners top-down programming, because they don't know which end is up. - C.A.R. Hoare

'Error, no keyboard ... press F1 to continue.' - Author Unknown