Just-in-time compilation - when does this happen in Java?

I recently took part in a discussion of Java performance. When I listened, many of the arguments against Java were that the interpretation was โ€œextremely time-consuming,โ€ and because of this, even simple Java programs run slower than similar ones compiled directly into machine code.

The answer to this question was that Java code is often compiled directly into machine code, if the JVM computes it, the program does it faster than if it were interpreted in a standard way.

My questions are: when does the JVM actually "decide" to execute the compilation "just in time"? What are the criteria that make JIT more efficient than standard bytecode interpretation? I mean, that compilation takes some time, and, as I understand it, all this should happen when the program is already running?

+6
source share
1 answer

This varies greatly depending on your JVM and its settings. Wikipedia:

For example, Sun Java Virtual Machine has two main modes - client and server. In client mode, minimal compilation and optimization is performed to reduce startup time. In server mode, extensive compilation and optimization is performed to maximize performance after application startup, sacrificing startup time. Other real-time Java compilers used a runtime measurement, which is the number of times that a method has performed in conjunction with a method byte size as a heuristic to decide when to compile. [4] Another uses the number of times performed in conjunction with loop detection. [5]

The rough approximation for the Vanilla HotSpot JVM in standby mode is that the JIT happens when the JVM notices that a particular method is called a lot, usually more than a certain number of times. (This is why the JVM is called "HotSpot" - because it identifies and optimizes the "hot spots" in your code.) Currently, the JVM knows a few things:

  • He knows that this method is worth the time to optimize, because, well, he gets a lot of names.
  • He knows a lot about the real characteristics of this function:
    • if one branch of the if much more common than the other, so it can improve branch prediction
    • if, for example, a List passed to this method is usually equal to ArrayList , so it can do optimizations and inlinings for the specific case of ArrayList .

Please note that if you compiled machine code in advance, you would not have much of this information for optimization - the compiler does not know in advance which paths are usually more common than others. But if you are waiting to do the optimization until you have real-world data, you can make better decisions.

More information on what JIT does is here .

+10
source

Source: https://habr.com/ru/post/951677/


All Articles