Java Virtual Machine - Garbage Collector and Memory Allocation Strategy (2)

  • We know that there are two ways to analyze whether an object is alive. One is the reference counting method. Although the reference counting method is simple and efficient, it is difficult to solve the circular reference. Therefore, the current mainstream virtual machines use the accessibility analysis method. In the reachability analysis method, the condition for the object to be reclaimed is that there is no reference to refer to it. To do this, you need to get all the GC Roots nodes to traverse from the GC Root. The main ones that can be used as GC Roots are global references (such as constants and static variables), and the execution context (the local variable table in the stack frame). So how to find the root node on the stack among so many global variables and local variable tables in the stack?

    Only a part of the data in the stack is of Reference type, and those data of non-Reference types are useless for finding the root node. If we scan the stack all over, it is quite a waste of time and resources.

    So what can be done to reduce the recycling time? We naturally think that space can be exchanged for time. We can record the position of the reference on the stack at a certain position, so that when gc occurs, we don't have to scan it all. In HotSpot, a method called OopMap is used. data structure to record. A simple understanding of OopMap is the object that stores debugging information.

    With the assistance of OopMap, we can quickly complete the GC Roots enumeration, but we cannot generate OopMap anytime and anywhere. On the one hand, it will require more space to store these objects, and on the other hand, the efficiency will be simple and low. So it will only be recorded in a specific location, mainly in:

    1. end of loop
    2. Before the method returns/after the call instruction of the calling method
    3. where exceptions may be thrown

    These locations are called safe spots.

    Regarding the safety point :
    we need to stop the jvm at a certain point in time when we are doing GC. If not, when we analyze the reference relationship between objects, the reference relationship is still changing. In this way our accuracy cannot be guaranteed. A safepoint is where all threads pause when they are about to be GCed. So how to make all threads stop at a safe point? There are two options here:

    1. preemptive interrupt
    2. Active interrupt

    In the preemptive interrupt, the active cooperation of the thread is not required. When the GC occurs, all threads are interrupted. If it is found that the interrupted place of the thread is not at the safe point, then the thread is resumed, and then it runs to the safe point.
    The active interrupt is to let the GC not directly operate the thread when it needs to interrupt the thread, set a flag, let each thread actively poll the flag, and let itself interrupt if the interrupt flag is true.

    Few virtual machines today use preemptive interrupts.

    About Safe Area :
  • A safe area refers to a piece of code where the reference relationship does not change, and it is safe to start GC anywhere in this area.
    Reprint:
    Author: MentallyL
    Link: https://www.jianshu.com/p/d0ab167b460d




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325640372&siteId=291194637