Today, I have a summary of common garbage collection algorithms, what we hear most is the garbage collection mechanism in the Java Virtual machine, in fact, the concept of garbage collection is not first proposed in Java,
The concept of garbage collection has long been raised and has been applied in other languages.
The garbage collection mechanism, which is no longer explained here, I mainly introduce the common garbage collection algorithm, of course, there are other.
algorithm one: reference counting method.
This method is one of the most classic points. In particular, to set a reference counter for the object, each increment of a variable reference to it, the reference counter will add 1, does not reduce the reference of a variable,
The reference counter is reduced by 1, and the object is recycled only if the object's reference counter becomes 0 o'clock. This algorithm is very simple, but there are often many problems, here I enumerate the two most obvious problems,
The first is to use this method, each time you increase the variable reference and reduce the reference to the addition or subtraction operation, if the object is frequently manipulated, to a certain extent, increase the consumption of the system.
The second is that this method cannot handle the case of circular references. Then explain what a circular reference is, assuming that there are two objects in a and b,a that reference the B object, and B also references a object,
At this point, the two-object reference counters are not 0, but due to mutual references, a and B cannot be garbage collected, causing a memory leak.
algorithm two: mark Clear method.
This method divides garbage collection into two phases: the tagging phase and the purge phase.
In the tagging phase, by following an object, marking all objects that start with the node, the Unlabeled object is a garbage object that is not referenced.
In the purge phase, the unmarked object is cleared out.
The disadvantage of this approach is that there may be a lot of disk fragmentation after garbage collection, which is exactly memory fragmentation. Because the object occupies an address space that is fixed. There are improved algorithms for this algorithm, which is the algorithm three I want to say later.
algorithm three: the Tag compression elimination method (Java middle-aged generation adopted).
On the basis of algorithm two, we make an improvement, it can be said that this algorithm is divided into three stages: marking stage, compressing stage, purging stage. The mark and purge phases do not change, but add a compression phase, after the marking phase is done,
Putting these tagged objects together and identifying the start and end addresses, such as all at the beginning, and then clearing, will not result in disk fragmentation. But we also have to notice a few problems, the compression phase takes up the system consumption,
And if you mark too many objects, the losses can be large and more efficient when tagged objects are relatively small.
algorithm four : Replication algorithm (in Java in the Cenozoic adoption).
The core idea is to divide the memory space into two pieces, use only one of them at a time, copy the surviving objects in memory that are in use to unused memory at garbage collection, and then clear all the objects in the memory block that is being used.
The unused block of memory is then turned into a block of memory that is being used, and the memory block that was used becomes an unused block of memory. Obviously, if there are more surviving objects, the algorithm will be less efficient, and this will make the memory space binary, but this method will not produce memory fragmentation.
algorithm Five : Generational method (Java heap adoption).
The main idea is to block the life cycle of the object, according to the characteristics of each block of memory, using different recovery algorithm, so as to improve the efficiency of garbage collection.
For example, the heap in the Java Virtual machine uses this method into the new generation and the old age. Then, different garbage collection algorithms are used for different generations.
The new generation uses the replication algorithm, and the old age uses the tag compression cleanup algorithm.
algorithm six : partitioning algorithm.
This method divides the entire space into successive different cells, each of which is used independently, and the benefit is that it can control how many small intervals are collected at a time.
Summary: Various recovery algorithms have their own advantages and disadvantages, no one algorithm can completely replace other algorithms, in the specific use should be combined with specific circumstances to choose.
Common garbage Collection algorithm