[In-depth understanding of the just-in-time compiler of the Java virtual machine] 1122 Compiled objects and trigger conditions

1. Hot code

A: The method that is called multiple times, the loop body that is executed multiple times.

2. Replace on the stack

The stack frame of the method is still on the stack, and the method is replaced => Hot code for the loop body

3. "Hot Spot Code Detection"

Know whether a certain piece of code is a hot code or whether it needs to trigger instant compilation, this behavior.

A: Sample Based Hot Spot Code Detection => It is difficult to accurately confirm the popularity of a method (affected by external factors)

The virtual machine periodically checks the top of the call stack of each thread. If it finds that a method (or some) often appears on the top of the stack, then this method is a "hot method".

B: Counter Based Hot Spot Code Detection. => Rigorous statistical results

The virtual machine establishes a counter for each method (even code block) to count the number of executions of the method. If the number of executions exceeds a certain threshold, it is considered a "hot method"

4. J9 uses the first sampling hot spot detection, and the second counter-based hot spot detection method is used in the HotSpot virtual machine

5. Two types of HotSpot counters:

A: Method invocation counter (InvocationCounter) and back edge counter (Back Edge Counter, "back edge" means to jump back at the loop boundary.

B: Method call counter (based on the number of times the method is called): Its default threshold is 1500 times in client mode and 10000 times in server mode.

Insert picture description here

The threshold can be set manually through the virtual machine parameter -XX: CompileThreshold

C: Back edge counter (in the bytecode when the control flow jumps backward, it is called "Back Edge"). It counts the number of executions of the loop body code in a method and triggers the replacement compilation on the stack.

Insert picture description here

Set the parameter -XX: OnStackReplacePercentage to indirectly adjust the threshold of the return counter

The virtual machine runs in the client mode, and the calculation formula of the return counter threshold is: method call counter threshold (-XX: CompileThreshold) multiplied by the OSR ratio (-XX: OnStackReplacePercentage) divided by 100.

Among them -XX: OnStackReplacePercentage default value is 933, if the default value is taken, then the threshold of the edge counter of the client mode virtual machine is 13995.

The virtual machine is running in server mode, and the calculation formula of the return counter threshold is: method call counter threshold (-XX: CompileThreshold) multiplied by (OSR ratio (-XX: OnStackReplacePercentage) minus interpreter monitoring ratio (-XX: InterpreterProfilePercentage) )) divided by 100.

The default value of -XX: OnStackReplacePercentage is 140, and the default value of -XX: InterpreterProfilePercentage is 33. If both are default values, then the threshold of the server-side virtual machine's return counter is 10700.

6. Counter Half Life Time (the method counter has a half life time)

Within a certain time limit, if the number of method calls is still not enough for it to be submitted to the just-in-time compiler for compilation, then the method call counter will be reduced by half. This process is called the counter decay of the method call counter.

Use the virtual machine parameter -XX: -UseCounterDecay to turn off heat decay and let the method counter count the absolute number of method calls.

Use -XX: CounterHalfLifeTime parameter to set the half-life cycle time, in seconds.

Note: Triggering compilation is not immediate replacement! Wait for the code to be compiled! The execution address of the method is replaced only when it is executed.

Guess you like

Origin blog.csdn.net/qq_40996741/article/details/109090021