Just in time compilation – when does it actually happen in Java?
Recently, I participated in a discussion on Java efficiency
The answer is that Java code is often compiled directly into machine code. If only the JVM calculates it, it will make the program faster than interpreting it in a standard way
My question is: when does the JVM "decide" to perform just in time compilation? What is the standard that makes JIT more effective than standard bytecode interpretation? I mean, compiling itself takes some time. As far as I know, should it happen when the program is already running?
Solution
It depends on your JVM and its settings Wikipedia:
For the vanilla hotspot JVM, a rough approximation of the in - server pattern is that when the JVM notices that a method has been called a lot, it usually exceeds a certain number of times, resulting in JIT (that's why the JVM is called "hotspot" – because it identifies and optimizes "hotspots" in code.) At this point, the JVM knows something:
>It knows that this method is worth the time to optimize because it will be called a lot. > It knows a lot about the real-world characteristics of this function:
>If one branch of an IF statement is more common than another, it can improve branch prediction > if the list passed to this method is usually an ArrayList, so it can be optimized and inlined according to the specific situation of ArrayList
Note that if you compile into machine code in advance, there won't be a lot of such information to optimize - the compiler doesn't know which paths are more common than others in advance However, if you wait for the actual data before optimizing, you can make better optimization decisions
More details about what JIT does are here