The algorithm implementation of hotspot

Source: Internet
Author: User

1. Enumerate the root nodesIn the accessibility analysis, a reference is found from the GC roots node, which can be used as a GC roots node primarily as a global reference and execution context, and if you want to check the references one after the other, the time will inevitably be consumed. In addition, the sensitivity of the accessibility analysis to the execution time is reflected in the GC pauses, as this analysis must be done in a snapshot that ensures consistency--"consistency" means that the entire system execution system appears to be frozen at a certain point in time throughout the analysis. It is not possible to show that the object reference relationship is still changing in the analysis process, and the accuracy of the analysis results cannot be guaranteed if the point is not satisfied. This is one of the important reasons why all Java execution threads must be paused when the GC is in progress.
since the current mainstream Java virtual machines are accurate GC, when the execution system pauses, do not need a non-leaking check execution context and global reference location, the virtual machine should have a way to know where the object is stored in the reference. In the implementation of the hotspot, a set of OOPMAP data structures is used to achieve this purpose. Oopmap will record what type of data is in the object's offset at the time the class is loaded, and in the JTI compilation process, the stack and register are referenced at a specific location. In this way, this information can be obtained directly when the GC is scanned. 2. Safety pointsWith Oopmap's assistance, the hotspot can quickly and accurately enumerate the GC roots, but it can lead to a very large number of instructions to change the reference relationship, and if you generate oopmap for each instruction, it will require a lot of extra space, so that the space cost of the GC becomes high. In fact, the hotspot does not generate oopmap for each instruction, it simply records the information in a specific location, known as the Security Point (SafePoint), where the program executes without pausing to execute the GC at all places. Can only be paused when the security point is reached. the selection of safepoint is neither too small to allow the GC to wait too long or to set it too frequently to increase the run-time load. so the setting of the security point is selected as the standard to let the program "have characteristics that let program time execute". The most obvious feature of "long execution" is the reuse of instruction sequences, such as method invocations, loop jumps, exception jumps, and so on, so instructions with these functions will produce safepoint.
Another problem with SafePoint is how to keep all threads running to a safe point when the GC occurs. There are two scenarios: preemptive interrupt and active interrupt. preemptive interrupts do not require thread code active mates, and when the GC occurs, all threads are first interrupted, and if the thread is found to be out of place on the security point, the thread is resumed, allowing him to run to the security point. There is now almost no virtual machine implementation that uses preemptive interrupts to pause threads in response to a GC. and the idea of active interrupt is when the GC needs to interrupt the thread, not directly to the thread operation, simply set a flag, each thread executes the active to poll this flag, found that the interrupt flag is true when the interrupt hangs itself, polling flag place and security point is coincident, Also add the memory where the object needs to be allocated.
3. Security ZoneThe use of security points seems to have solved the problem of how to get into GC, but the actual situation is not necessarily, the security point mechanism ensures that the program executes, in a not too long period of time will be entered into the safe point of the GC. But what if the program does not execute? the so-called program does not execute is not allocated CPU time, the typical example is that the thread is in sleep state or blocked state, when the thread is unable to respond to the JVM interrupt request, go to a safe place to suspend, the JVM is obviously unlikely to wait for the thread to reallocate CPU time, In this case, we use a security zone to solve.
A security zone is a piece of code that you don't change with a relationship. Starting the GC is safe anywhere in the region, and we can think of the security zone as an extended security point. When the thread executes the code in the security zone, it first identifies itself as having entered the security zone, so that when the JVM launches the GC, it does not have to identify itself as a thread of the security zone State. when the thread leaves the security zone, he checks to see if the system has completed the root node enumeration, and if it does, the thread will continue, otherwise he will have to wait until he receives a signal that it can safely leave the security zone.

This article is mainly I study Zhou Zhiming Teacher's "in-depth understanding of the Java Virtual Machine" a book of study notes, only for the study to consolidate the use of knowledge points, not for his use, in this thank teacher Zhou.


The algorithm implementation of hotspot

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.