-1

After searching on internet for a while, I didn't found a perfect answer that on which parameter(or flags) does JVM decide whether it should use JIT to compile the bytecode and run it or to interpret the bytecode.

In short is there any default out of this option, means if I say

> javac xyz.java    
> java xyz

and no other parameter what will run, JIT or interpreter?

The question is kept on hold for being too broad, so to shorten it up let me clarify I don't want to understand how JIT or interpreter works, neither do I want the "Procedure" how they decide what to either interpret or use JIT. I just want to know the parameters(may be program size,running environment etc) which are important while making this decisions.

Thanks for the answer in advance

Gaurav Jindal
  • 227
  • 4
  • 17
  • Although most modern JVMs have JIT support, it is not mandatory for all JVMs to provide JIT support. In most cases (although there are other techniques), JIT kicks in when a JVM determines that some part of the code (usually a method) has been called a certain number of times (JIT threshold). The HotSpot VM for example has two modes - `client` in which the JIT threshold is 1,500 (invocations of the same method) and `server` in which the threshold is 10,000. [This presentation](http://www.slideshare.net/ZeroTurnaround/vladimir-ivanovjvmjitcompilationoverview-24613146) provides more details. – manish Aug 31 '16 at 07:35
  • So that is the question, whenever I say java xyz, who takes the control first interpreter or JIT. As u said JIT is started after some a limit is reached, it means till then interpreter is working. Right? @manish – Gaurav Jindal Aug 31 '16 at 07:37
  • Can't understand why these people like: @aviad have put the question on hold. If you can't answer the question then atleast let other people discuss it. Discussion always produces some good results. – Gaurav Jindal Sep 01 '16 at 10:54

1 Answers1

1

javac, the java compiler always creates byte code (well, unless you talk about very special products that create static "native code" binaries out of java sources).

For compiling to native code within the JVM -that decision happens at runtime!

Meaning: the JVM starts of interpreting bytecode. It keeps statistics while doing that. Depending on various heuristics, the JVM/JIT will decide at some point to compile byte code into native code. That means: when the JVM process ends, all that native code is lost!

The one thing that makes JIT such an interesting technology is the fact that, if things change, the JIT can even "re-compile" to native; if it figures that different optimizations would make sense.

Further reading can be found here for example.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • So from your answer can we say by default JIT is in control and it runs different execution plans to find which is best, JIT or interpreter? – Gaurav Jindal Aug 31 '16 at 07:22
  • Well, the JIT being a component within the JVM. I guess one could say so. See my updated answer. – GhostCat Aug 31 '16 at 07:23
  • The only thing that JVM can guarantee is that if feature X is turned off, it will not be used. But if it is turned on, there is no guarantee that it will be used. In fact, for 2 different runs of the same code, there is no guarantee that the same optimizations will be applied – TheLostMind Aug 31 '16 at 07:34
  • Sorry, but I am bit confused, because I read this answer previously before posting my question: http://stackoverflow.com/a/16440092/4123672 . The answer here says Interpreter is first started. – Gaurav Jindal Aug 31 '16 at 07:34