hotspot Just-in-time compilers in virtual machines
Recently in the Zhou Zhiming teacher's "in-depth understanding of the Java Virtual machine" This book, after looking at the feeling of a lot of harvest, so write a point of reading Summary it.
First look at the following questions: 1, why hotspot Virtual Confidential use interpreter and compiler coexist architecture. 2, why hotspot Virtual Confidential to achieve two different just-in-time compilers. 3. When the program is executed using the interpreter. When to use the compiler to execute. 4, those program code will be compiled into local code. How to think of local code.
Java programs are initially interpreted only through the interpreter, that is, the execution of bytecode-by-article interpretation, which is relatively slow to execute, especially if a method or block of code is running particularly frequently, this approach is inefficient. Then in the virtual machine to introduce a JIT compiler (Just-in-time compiler), when the virtual machine found that a method or block of code to run particularly frequently, it will be identified as "Hot Spot Code" (hotspot codes), in order to improve the efficiency of hot code execution, at runtime, The virtual machine will compile the code into the machine code associated with the local platform and perform various levels of optimization, which is the JIT compiler that completes the task.
The following is a question-and-answer statement 1, why hotspot Virtual Confidential use the interpreter and compiler coexistence architecture.
Now mainstream commercial virtual machines (such as Sun HotSpot, IBM J9 contains both interpreters and compilers (JRockit, one of the three commercial virtual machines, is an exception, and there is no interpreter inside it, so there are drawbacks such as a long boot time, but it is mainly service-oriented applications, This type of application generally does not focus on startup time. Each has its advantages: when a program needs to be launched and executed quickly, the interpreter can play a role first, save the compile time, execute immediately, when the program runs, over time, the compiler will gradually play a role, the more and more code compiled cost code, you can achieve higher execution efficiency. interpreting execution can save memory, while compilation execution can improve efficiency . In the entire virtual machine execution architecture, the interpreter works with the compiler often. 2, why hotspot Virtual Confidential to achieve two different just-in-time compiler reason ...
There are two just-in-time compilers built into the hotspot virtual machine: Client complier and Server complier, referred to as C1, C2 compilers, respectively, for clients and servers. The current mainstream hotspot virtual machine is by default the interpreter works directly with one of the compilers. which compiler the program uses depends on the mode in which the virtual machine is running. Hotspot Virtual Opportunities Automatically select Run mode based on its version and host and its hardware performance, and users can also use the "-client" or "-server" parameters to force the specified virtual machine to run in either client or server mode. get higher compilation speed with client complier, and use server complier to get better compilation quality . 4, those program code will be compiled into local code. How to compile to local code.
Code in the program is only hot code, will be compiled into the local code, then what is the hotspot code.
There are two classes of "hotspot code" that are compiled by the Just-in-time compiler during the run : methods that are called multiple times. The loop body that is invoked multiple times.
In both cases, the compiler is a compilation object of the entire method, which is also the standard compilation method in the virtual machine.
How to judge a method or a piece of code or not a hot code?
To know whether a method or a piece of code is not hot code, it is not necessary to trigger Just-in-time compilation, Hot spot detection (hotspot detection) is required. At present, the main hot spot to determine the following two kinds of ways:
(1) Hot spot detection based on sampling
This method of virtual opportunity to periodically check the top of each thread, if you find that some methods often appear on the top of the stack, the method code is "hot code." The advantage of this approach is that it is simple and efficient, and it is easy to get a method invocation relationship, and the disadvantage is that it is difficult to accurately identify the heat of a method, and it is easy to disrupt hotspot detection because of thread blocking or other external factors.
(2) Hot spot detection based on counter
Using this method of virtual opportunity for each method, or even the code block set up counters, statistical method of execution times, if the number of executions over a certain threshold, it is considered a "hot method." This statistic method is more complicated, it needs to establish and maintain counters for each method, and it can't get the call relation of method directly, but its statistic result is more precise and rigorous. what is the hot spot detection method used in the hotspot virtual machine?
The second-hotspot hotspot detection method is used in the virtual machine , so it prepares two counters for each method: method call counter and back-side counter . On the premise of determining the operating parameters of the virtual machine, both counters have a defined threshold, which triggers JIT compilation when the counter exceeds the threshold overflow.
method Call Counter : This counter is used to count the number of times the method was invoked. 3. When the program is executed using the interpreter. When to use the compiler to execute.
When a method is invoked, the method is first checked for the existence of a JIT-compiled version, and, if it exists, is executed using the compiled local code preferentially. If no version has been compiled, add the call counter value of this method by 1 and then the half method invokes the threshold of the method call counter after the counter and the back counter value. If you exceed the threshold, you will want the Just-in-time compiler to submit a code compilation request for the method.
If no setting is made, the execution engine does not wait for the compilation request to complete, but instead continues with the interpreter executing the bytecode as interpreted until the submitted request is compiled by the compiler. When the compilation is complete, the call entry address for this method is automatically overwritten with the new one, and the compiled version is used the next time the method is called.
Back-edge counter: Statistics the number of times a method executes the loop body code. It is not described here, as is the principle of the back counter of the loop body and the method counter.