2

I have some confusion about compilation and Interpretation and also the way JIT works. I know that the source code or the Java Program gets compiled to Java Byte Code which is then loaded onto JVM which then either interprets the bytecode to native machine code or uses JIT.

I was going through Interpreter doc and understood that an Interpreter analyses each statement on every request and then converts to native code leading to slower performance.

And also read that JIT does dynamic translation and stores the native code on a cache which is then used for subsequent requests.

What I wanted to understand was that how exactly JIT works? Whenever a first request is made to an application then it converts that part of byte code to machine code, stores on cache and uses it.. Is this the process? If so then, is every first request always slower as it needs translation of bytecode to machine code?

Please explain this in detail.

user001
  • 991
  • 6
  • 16
  • 34

1 Answers1

2

Depends on the implementation, but at least in Java, JIT is usually done for "hotspots", i.e. frequently used code. So if the JVM finds out that a method has been called more than X times it will compile it to native code. Meaning, you will have to call it a few times first, and it only gets faster after that.

Also, JIT results are usually kept in-memory, not saved to disk, so that the process starts from scratch the next time the application runs (that is a big difference between Just-in-Time and Ahead-of-Time compilation, the latter happening before the program runs).

understood that an Interpreter analyses each statement on every request and then converts to native code leading to slower performance.

well, you take the performance hit waiting for the compilation to happen just once. After that it is executing the compiled native code for the method, and before that it is interpreting the bytecode. If the JVM's guess as to this method being a hotspot was right, it should be pay off over time.

Is the first request to a Java Application always slow even with JIT Compiler?

If anything, the presence of the JIT compiler makes the first (or a couple of early) requests slower, because it adds extra work. However, subsequent requests should be much faster than for a non-JIT execution environment.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Hi Thilo, Thanks for your answer. Could you also please tell which is the best approach to go? From your answer I came to know that even JIT needs multiple access to the code to convert it to native. So Is there any best approach which can improve the performance? – user001 Oct 08 '13 at 01:45
  • You need to give *a lot* more details about where your performance problem is. Is it fast enough after JIT has been done? – Thilo Oct 08 '13 at 01:51