tags: # java virtual machine jvm GC
This article covers a brief introduction to several commonly used garbage collection algorithms, including:
The content and screenshots are all from "Practical Java Virtual Machine: JVM Troubleshooting and Performance Optimization (2nd Edition)"
The mark-and-sweep method is the ideological basis of modern garbage collection algorithms. The mark-and-sweep method divides garbage collection into two phases: the mark phase and the sweep phase.
In the marking phase, all reachable objects starting from the root node are first marked by the root node. Therefore, unmarked objects are unreferenced garbage objects. Then, in the cleanup phase, all unmarked objects are cleaned up. The biggest problem with the mark-and-sweep method is the potential for space debris.

The core idea of the copy algorithm is: Divide the original memory space into two blocks, and only use one of them each time. During garbage collection, copy the surviving objects in the memory being used to the unused memory block, and then Clear all objects in the memory block being used, swap the roles of the two memories, and complete garbage collection.
If there are many garbage objects in the system, the number of surviving objects that the replication algorithm needs to replicate will be relatively small. Therefore, at the moment when garbage collection is really needed, the efficiency of the replication algorithm is very high.
And because the objects are uniformly copied to the new memory space during the garbage collection process, it can be ensured that the recovered memory space is not fragmented. Although it has the above two advantages, the cost of the copy algorithm is to halve the system memory. Therefore, the simple copy algorithm is also difficult to accept.

In Java's new generation SerialGC collector, the idea of copy algorithm is used. The new generation is divided into three parts: eden area, from area and to area. Among them, the from area and the to area can be regarded as two spaces with the same size, equal status and interchangeable roles for replication. The from area and to area are also called the survivor area, that is, the survivor space, which is used to store objects that have not been recycled.
During garbage collection, the surviving objects in the eden area will be copied to the unused survivor area (assumed to be the to area), and the young objects in the in-use survivor area (assumed to be from) will also be copied to the to area (large objects Or the old object will directly enter the old age, if the to area is full, the object will also directly enter the old age). At this time, the remaining objects in the eden area and the from area are garbage objects, which can be emptied directly, and the to area stores the surviving objects after this recycling. This improved copy algorithm not only ensures the continuity of space, but also avoids a lot of waste of memory space, as shown in the figure below.

Mark compaction is an old generation collection algorithm. It has made some optimizations based on the mark-and-sweep method. Like the mark removal method, the mark compression method first needs to start from the root node and mark all reachable objects once. But then, instead of simply cleaning up unmarked objects, it compacts all surviving objects into one end of memory. Then, clean up all space outside the bounds. This method not only avoids fragmentation, but also does not require two identical memory spaces, and is cost-effective.

Due to the different characteristics of the new generation and the old generation, different algorithms are used for different generations.
The new generation generally uses the replication algorithm, for example, the following garbage collectors are based on the replication algorithm:
The old generation generally uses mark-sweep or mark-compact algorithms, such as the following garbage collectors:
The partitioning algorithm divides the entire heap space into different consecutive small areas, as shown in the following figure. Each cell is used independently and recycled independently. The advantage of this algorithm is that it can control the number of reclaimed cells at one time.
Generally speaking, under the same conditions, the larger the heap space, the longer the time required for a GC, and the longer the resulting pause. In order to better control the pause time generated by GC, a large memory area is divided into multiple small blocks. According to the target pause time, several small areas are reclaimed reasonably each time instead of reclaiming the entire heap space, thereby reducing one GC resulting pause.

The G1 collector (GarbageFirst) is a new garbage collector officially used in JDK1.7. From a long-term perspective, it is to replace the CMS collector. The G1 collector has a unique garbage collection strategy, which is completely different from the previously mentioned collectors.
From the point of view of generation, G1 is still a generational garbage collector. It will distinguish the young generation from the old generation, and still have the eden area and the survivor area. Or the old generation is continuous.
It uses partitioning algorithm. As a long-term alternative to CMS, G1 uses a brand-new partition algorithm with the following features:
First, the algorithm introduction 1. What is the algorithm? The algorithm refers to the accurate and complete description of the solution, and is a series of clear instructions to solve the problem. T...
background knowledge: Hash algorithms are mainly used in cache distributed systems. Distributed means that a whole is divided into multiple parts, which are executed by multiple servers. Pay attention...
Introduction As a developer, it is necessary to understand some encryption algorithms. Through encryption algorithms, we can accomplish three goals of data communication, namely data confidentiality, ...
Insertion sort(Insertion sorting): Insert a piece of data into ordered data to form new ordered data with a length plus one. It is suitable for sorting a small amount of data. The time complexity is O...
Common GC algorithms Reference counting: Every object has a counter, the object is referenced once, the counter is +1, when the object reference fails once. The counter -1, when the object counter is ...
1. How to judge whether an object is dead in java? Death means it can be recycled 1. Reference counting algorithm: Add a reference counter to each object. When a reference points to an object, the cou...
1. Common GC algorithms 1. Mark-clearing algorithm Mark the objects that need to be recycled and then clean them directly in GC. Disadvantages: It will cause a large number of discontinuous memory fra...
concept What is machine learning? Machine learning is a literal translation of the English name Machine Learning (ML). Machine learning involves many disciplines such as probability theory, statistics...
data encryption Foreword 1.MD5 encryption 2.RSA encryption 3. AES encryption 4. 3DES encryption Foreword Whether user data is stored in SharePreferences or in a SQLite database, it is necessary to enc...
2019 Unicorn Enterprise Heavy Gold Recruitment Python Engineer Standard >>> Bicubic interpolation (also called coordination plate element), the binary bicubic interpolation formula has a tota...