Hotspot Algorithm Implementation

Source: Internet
Author: User

1. Enumerate the root nodes

The nodes that can be used as GC roots are mainly in global references (such as Constants or class static properties) and execution contexts (such as table of local variables in a stack frame).

The sensitivity of the accessibility analysis to the execution time is reflected in the GC pauses, because the analysis work must be done in a snapshot that ensures consistency, that is, the object reference relationships are constantly changing during the parsing process, so this is the Java execution thread that will have to pause all the time when the GC is in progress.

If, after the execution of the system pauses, a non-leak check all the execution context and the global reference location, the pause time must be very long (for some large-scale applications, the pause time unbearable), so the virtual machine should have a way to know directly where the object reference, in the implementation of the hotspot, is to use a set of data structures called Oopmap to achieve this purpose, when the class loading is completed, the object what offset is what type of data calculated, during the JIT compilation process, also in the specific location of the stack and register where is the reference, so, The GC can be directly aware of this information when it is scanned.

2. Safety points

With the help of Oomap, hotspot can complete GC roots enumeration quickly and accurately, but there is another problem, oopmap content changes the instructions very much, if each one of the instructions generated corresponding OOPMAP, will require a lot of extra space.

The hotspot does not generate oomap for each instruction, it simply records the information in a "specific location", which is called a security point, where the GC is not stopped in all places when the program executes, only the security point can be paused, the security point cannot be selected too small to allow the GC to wait too long, It cannot be too frequent to increase the load at run time.

How can a GC occur when all threads (except the thread that executes the JNI call) pause to the nearest security point?

1. Preemptive interrupt, do not need the execution code of the thread to actively cooperate, when the GC occurs, all the threads are interrupted first, if the thread break is not in the security point, then restore the threads, let it execute to the security point.

2. Active interrupt, need to interrupt the thread, do not directly to the thread operation, but set a flag, each thread executes actively polling this flag, found that the interrupt flag is true when the interrupt hangs. The polling flag is coincident with the place and the security point.

3. Security Zone

The security point mechanism guarantees that when the program executes, it will encounter a security point that can enter the GC in a not too long time, but when the program "does not execute", the program does not execute or allocates CPU time, the thread cannot respond to the JVM's interrupt request, and the JVM is apparently unlikely to wait for the thread to be allocated CPU time again.

A security zone is a piece of code that does not change the reference relationship. It is safe to start a GC anywhere in the region.

When a thread executes to a security zone code, it first identifies itself to the security zone, where the JVM initiates a GC and does not have to be labeled as a security zone. When the thread is leaving the security zone, check to see if the system has completed the root node enumeration, and if it completes, it will continue to wait until it receives a signal that it can safely leave the security zone.

Hotspot Algorithm Implementation

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.