Learn the GC algorithm together (detailed graphic and text)

Hello, I’m Chenxi. I’m glad you can read it. This article sorts out the knowledge points related to GC, which is the garbage collection algorithm. I hope it will be helpful to you, share new knowledge, and make progress together!


1. Preface

GC (Garbage Collection): Garbage collector in JAVA/.NET. Java is developed from C++. It discards some cumbersome and error-prone things in C++. One of them is this GC.


A significant advantage of the Java language is the introduction of a garbage collection mechanism, which solves the problem of memory management to a certain extent.

Garbage collection can effectively prevent memory leaks and effectively use free memory;


What is the principle of the garbage collection mechanism?
Insert picture description here

In fact, the principle of the garbage collection mechanism is to use some algorithms to manage memory, so as to effectively prevent memory leaks and effectively use free space (memory space).


Two, GC algorithm

The Java language specification does not specify which garbage collection algorithm the JVM uses, but any garbage collection algorithm generally does two basic things:

1. Find useless objects;

2. Reclaim the memory space occupied by useless objects. Make the space available for use by the program again;

Find the recycling object---->When to recycle---->How to recycle---->Release


How to find these useless objects?

1. Counting method (Reference Counting Collector)

Add a counter to the object, if it is referenced, the counter is increased by one; if the reference is released, the counter is decreased by one. When the counter is zero, it means that the object is not referenced and needs to be recycled.

What if two objects refer to each other? For example, A refers to B, and B refers to A, A.reference=B;B.reference=A, then it cannot be released. Therefore, this algorithm has not been adopted.

This method is not used by Java, but Python uses this algorithm.

to sum up Explanation
advantage The reference counting collector can be executed very quickly, intertwined in the running of the program. It is more beneficial to the real-time environment where the program does not need to be interrupted for a long time
Disadvantage Unable to detect circular references. If the parent object has a reference to the child object, the child object in turn refers to the parent object. In this way, their reference count can never be 0

2. Root search algorithm

(Reachability analysis) Set up several kinds of root objects. When any root object to a certain object cannot be reached, then this object is recyclable.
Insert picture description here
The white part on the right side of the above figure indicates that the root cannot be reached, that is, it will be judged as a garbage collection object

In the Java language, there are the following objects that can be used as GC roots:

1. Referenced objects in the virtual machine stack.

2. The object referenced by the class static property in the method area.

3. The object referenced by the constant in the method area.

4. Objects referenced by JNI in the local method stack.

The first and fourth methods both refer to the local variable table of the method. The second expression has a clearer meaning, and the third mainly refers to the constant value declared as final.


Common GC algorithm?

Insert picture description here

1. Copying algorithm (Compacting Collector)

Simply put: First find objects that cannot be recycled, put them aside, and then sweep away the objects that can be recycled!

Insert picture description here
The replication algorithm divides the memory into two sections. At any point in time, all dynamically allocated objects can only be allocated in one section (called the active section), and the other section (called the free section) is free.

When the effective memory space is exhausted, the JVM will suspend the program and start the replication algorithm GC thread. Next, the GC thread will copy all the surviving objects in the active range to the free range, and arrange them strictly according to the memory address. At the same time, the GC thread will update the memory reference address of the surviving object to the new memory address.

To use the replication algorithm, at least the survival rate of the object must be very low, and most importantly, we must overcome 50% of the waste of memory.


2. Mark-clear algorithm.

The mark-and-sweep algorithm uses scanning from the root set to mark the surviving objects. After the mark is completed, the unmarked objects in the entire space are scanned for recycling, as shown in the figure.

Insert picture description here
The mark-sweep algorithm does not need to move objects, and only processes non-surviving objects. It is extremely efficient when there are many surviving objects. However, because the mark-sweep algorithm directly reclaims non-surviving objects, it will cause memory fragmentation. .

The disadvantage is that it will produce very discontinuous memory fragments. For example, if you are looking for a 4K storage, the current small square 1K storage will trigger a GC if it can't be found!

Summary: mark first ---- then clear


3. Marking-sorting algorithm

The mark-organization algorithm uses the same method as the mark-sweep algorithm to mark objects, but it is different when clearing. After reclaiming the space occupied by non-surviving objects, all surviving objects will be moved to the left free space, and the corresponding ones will be updated. pointer. The mark-organize algorithm is based on the mark-clear algorithm, and the object is moved, so the cost is higher, but it solves the problem of memory fragmentation

Insert picture description here
Summary: Mark first-then clear-then sort


4. Generation algorithm (Generational Collector)

Generational collection algorithm: In fact, we have to understand that most objects are living and dying (90%)

The generational garbage collection strategy is based on the fact that the life cycle of different objects is different. Therefore, objects of different life cycles can adopt different recycling algorithms to improve recycling efficiency.
Insert picture description here

Expansion of ps:
①Eden area (80%) and two Survivor areas (10%), the new generation and old generation in the heap account for 1:2
②Each time Eden and one Survivor are used, the surviving objects will be copied at once when recycling To another piece of Survivor, if another piece of Survivor has insufficient space, the allocation guarantee mechanism is used to store it into the old age. When to enter the old age from Survivor, it depends on the type of garbage collector.

Process: Divide the heap into the new generation and the old generation, and choose different collection algorithms according to the regional characteristics. If the new generation is born and die, the copy algorithm will be used, and the old generation will use mark removal or mark sorting.


3. Expansion summary

Finally, I use a mind map to summarize this article, let's review together whether we still remember the relevant points!

Insert picture description here
The specific advantages and disadvantages of each algorithm are shown in the figure. If you have any questions, please communicate with us!


Expansion summary: summary of the root search algorithm

1. The root search algorithm solves the basic problem of garbage collection, and the most critical problem is which objects can be recycled;

2. Garbage collection obviously needs to solve when and how to recycle;

3. On the basis of the root search algorithm, in the implementation of modern virtual machines, there are three main garbage collection algorithms, namely mark-sweep algorithm, copy algorithm, and mark-sort algorithm. These three algorithms have expanded the root search algorithm ;


This article is just a brief introduction to GC, and there are more related issues about virtual machine tuning that are worth exploring and learning!


The best investment is to invest in yourself.

Insert picture description here

2020.09.20 May you all go to be in your love!

Guess you like

Origin blog.csdn.net/weixin_45393094/article/details/108691358