The Java program is initially interpreted by the interpreter, and when the virtual machine discovers that a method or block of code is running particularly frequently, the code is identified as "Hot Spot Code". In order to improve the efficiency of hot code execution, at run time, the virtual opportunity to compile the code of the cost platform-related machine code, and to perform various levels of optimization, the compiler to complete this task is called the Instant compiler (JIT compiler, not a necessary part of the Java Virtual machine).
To understand the operation of the instant compiler within a hotspot virtual machine, there are several issues to solve:
- Why does the hotspot virtual machines use the interpreter and compiler coexistence architecture?
- Why do the hotspot virtual machines implement two different instant compilers?
- When does the program use the interpreter? When do I use the compiler?
- What program code will be compiled with cost code? How do I compile?
- How can I see the compilation and compilation results of the instant compiler from the outside?
0.1. Interpreter and Compiler:
The interpreter is a one-piece explanation of the execution source language. For example, Php,postscritp,javascript is a typical interpretive language. ( direct execution )
The compiler compiles the source code all the way to the target code , executes it no longer requires the compiler, and runs directly on the platform that supports the target code, which is much faster than the interpretation execution. For example, C language code is compiled into binary code (EXE program), executed on the Windows platform.
When a program needs to be started and executed quickly, the interpreter can work first, omitting the compile time and executing it immediately. After the program runs, as time goes by, the compiler gradually works, and more and more code is compiled with cost code, which can get better execution efficiency. The interpreter saves memory and the compiler is more efficient. The interpreter can also act as an "escape gate" for the compiler's aggressive optimization operation, and when the radical optimization hypothesis is not established, it is returned to the state of interpretation to continue execution.
0.2 Why the hotspot virtual machines implement two different instant compilers:
The hotspot includes two compilers, client compiler and server complier, or C1 and C2 compilers, respectively. Using the two compiler's layered compilation (tiered compilation) policy, C1 and C2 work at the same time, some code may compile multiple times, C1 get higher compilation speed, C2 get better compile quality :
- The No. 0 layer, the program interpretation execution, the interpreter does not turn on the performance monitoring function (Profiling), can trigger 1th layer compilation.
- The 1th layer, also known as C1 compilation, compiles bytecode into code for simple, reliable optimization, adding the logic of performance monitoring if necessary.
- The 2nd layer, also known as C2 compilation, also compiles bytecode into native code, but it starts some compile-time optimizations and even performs some unreliable aggressive optimizations based on performance monitoring.
0.3
Compile objects and trigger conditions
There are two types of "hotspot code" that are compiled by the instant compiler during the run:
- Methods that are called multiple times
- The loop body that was executed multiple times
In the first case, because the compilation is triggered by a method call, the compiler takes the entire method as the compiled object, which is the standard JIT-compiled method. The latter, although it is the compilation action triggered by the loop body, the compiler still follows the entire method (rather than the individual loop body) as the compilation object. This type of compilation is known as stack substitution (on stack replacement, which is referred to as OSR compilation).
Determine if a piece of code is not hot code, it is necessary to trigger the immediate compilation, such behavior is called hotspot detection (hot spot Detection), there are two ways:
- Sampling-based hotspot detection: the virtual opportunity of such a method periodically checks the top of each thread's stack, and if a certain (or some) method is found to often appear on top of the stack, this method is a "hot spot". The advantage is that it is simple, efficient, and easy to get a method call relationship (the call stack can be expanded), the disadvantage is that it is difficult to accurately determine the heat of a method, because it is susceptible to thread blocking or other external factors.
- Hot spot detection based on counter: For each method (even code block) to establish counters, statistical methods of execution times, more than a certain threshold is considered "hot method." The disadvantage is that it is more cumbersome to implement, it is necessary to establish and maintain counters for each method, and it is not possible to get the call relationship directly to the method, the advantage is that its statistical results are relatively more precise and rigorous.
Hotspot virtual machine using the second, it prepares two types of counters for each method: The method call counter (invocation Counter) and the back Edge Counter, which is used to count the number of times the loop body code executes in a method.
This article from: 73281722
Java JIT (Just in time)