In-depth understanding of Java Virtual machines: Instant compilers within a hotspot virtual machine

Source: Internet
Author: User

Instant compilers within the hotspot virtual machine

Recently in See Zhou Zhiming Teacher's "in-depth understanding of Java Virtual Machine" This book, after looking at a lot of harvest, so write a summary of reading it.

Let's look at the following questions:

  • 1. Why does the hotspot virtual confidential use the interpreter and compiler coexist architecture?
  • 2, why the hotspot virtual Confidential implementation of two different instant compilers?
  • 3. When does the program use the interpreter? When do I use the compiler?
  • 4, those program code will be compiled into local code? How do you think of local code?

Java programs are initially interpreted only by the interpreter, that is, the execution of bytecode-by-article interpretation, which is relatively slow to execute, especially when a method or block of code is running particularly frequently, and this approach is inefficient. So later in the virtual machine introduced the JIT compiler (instant compiler), when the virtual machine found a method or block of code run particularly frequently, it will be identified as "Hot Spot Code", in order to improve the efficiency of hot code execution, at run time, The virtual machine will compile this code into the machine code associated with the local platform and optimize it at all levels, and it is the JIT compiler that accomplishes this task.

The following are described in question and answer form

  • 1, why the hotspot virtual confidential use of the interpreter and compiler co-existence of the architecture??

Now almost all of the mainstream commercial virtual machines (such as Sun HotSpot, IBM J9) contain both interpreters and compilers (the jrockit of one of the three commercial virtual machines is an exception, there is no interpreter inside, so there are drawbacks such as the startup time, but it is mainly service-oriented applications, This type of application typically does not focus on startup time). Each has the advantage: when the program needs to start and execute quickly, the interpreter can play a role first, save the time of compiling, immediately execute; When the program runs, as time goes by, the compiler will gradually play a role in compiling more and more code to compile the cost code, can get higher execution efficiency. interpreting execution can save memory, and compiling execution can improve efficiency . In the entire virtual machine execution architecture, the interpreter and the compiler often work together.

  • 2, why the hotspot virtual Confidential implementation of two different real-time compiler reason???

There are two instant compilers built into the hotspot virtual machine: Client complier and Server complier, referred to as C1, C2 compilers, respectively, used on clients and servers, The default in the current mainstream hotspot virtual machine is to use an interpreter to work directly with one of the compilers. which compiler the program uses depends on the mode in which the virtual machine is running. The hotspot virtual machine automatically chooses the operating mode based on its version and host and its hardware performance, and the user can also use the "-client" or "-server" parameter to force the specified VM to run in client mode or server mode. Use the client complier to get a higher build speed and use the server Complier to get better compilation quality .

  • 4, those program code will be compiled into local code? How do I compile to local code?

The code in the program will only be compiled into local code when it is hot code, so what is hot code?

There are two types of "hotspot code" that will be compiled by the instant compiler during the run :

  • The method that was called multiple times.
  • The loop body that is called multiple times.

In both cases, the compiler takes the entire method as the compilation object, and this compilation is also the standard way to compile the virtual machine.
How to judge a method or a piece of code or is it a hotspot code?
To know whether the method or a piece of code is not hot code, it is necessary to trigger the immediate compilation, need to do hot spot Detection (hotspot detection). At present, the main hot spot determination methods have the following two kinds:

(1) Hot spot detection based on sampling

The virtual opportunity of this method periodically checks the top of each thread, and if some methods are found to be present at the top of the stack, then this method code is the "Hotspot code". The advantage of this detection method is that it is simple and efficient, and it is easy to get the method call relationship, the disadvantage is that it is difficult to accurately confirm the heat of a method, it is easy to disturb the hotspot detection because of the thread blocking or other external factors.

(2) Hot spot detection based on counter

Using this approach, virtual opportunities for each method, or even code blocks to build counters, statistical methods of execution times, if the number of executions exceed a certain threshold, it is considered to be "hot method." This kind of statistical method is more complicated, it needs to establish and maintain counter for each method, and can't get the call relation of method directly, but its statistic result is more precise and rigorous.

What clock hotspot detection is used in hotspot virtual machines?

The second, counter-based hotspot detection method is used in the hotspot virtual machine , so it prepares two counters for each method: The method call counter and the back edge counter . Both counters have a defined threshold when determining the virtual machine's operating parameters, which triggers JIT compilation when the counter exceeds the threshold value.

method Call Counter : This counter is used to count the number of times the method is called.

  • 3. When does the program use the interpreter? When do I use the compiler?

When a method is called, it first checks if the method has a JIT-compiled version, and if it does, it takes precedence over the compiled native code. If a version that has already been compiled does not exist, the call counter value of this method is incremented by 1, and then the half-segment method calls the counter and the back-edge counter value beyond the threshold value of the method call counter. If the threshold is exceeded, the immediate compiler is expected to submit a code compilation request for the method.

If you do not make any settings, the execution engine does not wait for the compilation request to complete, but continues the interpreter to execute the bytecode as interpreted until the submitted request is compiled by the compiler. When the compilation is complete, the call entry address of the method is automatically rewritten to the new one, and the compiled version is used the next time the method is called.

Back Edge counter: counts the number of times a loop body code is executed in a method. The back side counter of the application and the loop body is the same as the principle of the method counter, which is not described here.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

In-depth understanding of Java Virtual machines: Instant compilers within a hotspot virtual machine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.