JVM heap memory model town building.

Read the chapter 3 GC algorithm of “In-depth Understanding of Java Virtual Machine”. The paragraph about GC Roots enumeration was not explained thoroughly, so I encountered confusion in understanding. Therefore, expand and record this point, and find that all kinds of blogs in China write almost the same analysis, or do not clarify the confusion: how GC Roots enumerate, what kind of data structure OopMap is used in it?

Recap: Three major GC algorithms

1. Mark-sweep algorithm

2. Copying Algorithms

3. Mark-compact algorithm

The three algorithms all use the reachability analysis method to clear the mark of the object.

The accessibility analysis method is divided into three steps:

Step One: Enumerations of Roots.

Find the roots of the vine.

Step Two: Marking all object references starting from the roots.

Start at the root and follow the trail. Every time you touch a melon, mark it — don’t pick it until it’s ripe.

Step Three: Sweeping all the unmarked objects.

Melons are ripe, and those that are not marked are naturally ripe melons, so take them away and eat them.

The pseudo-code corresponding to the above process is as follows:

Void GC() {

    HaltAllProcessing();
    
    ObjectCollection roots = GetRoots();
    for(int i=0; i<roots.Count(); ++i) {
        Mark(roots[i]);
    }
    
    Sweep();
    
}
Copy the code

Q: What is GC Roots?

For your application code to reach an object, there should be a root object which is connected to your object as well as capable of accessing from outside the heap. Such root objects that are accessible from outside the Heap are called Garbage Colllection (GC) roots. There are several types of GC roots such as Local variables, Active Java threads, Static variables, JNI References etc. (Just take the ideology here, if you do a quick google search, you may find many conflicting classifications of GC roots)

For a Java program, objects are stored in the heap, and the remaining objects are referenced by the root node. GC Roots are reference types that are not in the heap. Where can I put them?

The answers are placed on the stack, including:

Local variables Local variables

Static variables Static variables

JNI References JNI References etc

How to quickly enumerate GC Roots?

1, stupid method: go through all the variables in the stack, judge the type one by one, if it is the Reference type, it belongs to GC Roots.

2. Efficient method: Externally record the type information of those Reference type variables in the stack and store it in a mapping table — this is the origin of OopMap.

“When interpreting execution /JIT, record the data type corresponding to some data on the stack, such as the value ‘12344’ at address 1 is a heap reference of data type com.aaa.aaA.aaa)

The three major high-performance JVM implementations, HotSpot, JRockit, and J9, do this. HotSpot calls these data structures OopMap, Livemap in JRockit and GC Map in J9.”

When GC, you can quickly implement the root node enumeration directly from this OopMap.

Reference links:

Understanding Java Garbage Collection

The JVM OopMap