Common algorithms for garbage collection
1. Reference counting method
1.1 Principle
Suppose there is an object A, any object references A, then the reference counter of object A is +1, when the reference is invalid, the reference counter of object A is -1, when the reference counter of object A is 0, it means that object A has Use quoted, then you can recycle.
1.2 Advantages and disadvantages
advantage:
-
The real-time performance is high, and there is no need to wait until the memory is insufficient before starting to recycle. At runtime, it is judged whether the object counter is 0 and recycling is performed.
-
The application does not need to be suspended during the garbage collection process. If the memory is insufficient when applying for memory, an outofmember error will be reported immediately.
-
Regional, when updating the object counter, only the object will be impressed, not all objects will be scanned.
Disadvantages
-
Every time an object is referenced, you need to update the object counter, which will increase the time overhead.
-
A waste of CPU resources, because the memory is enough, the object counter is still running statistics.
-
Unable to solve the circular reference problem (the biggest disadvantage)
Circular references such as: Object A maintains a member attribute, the type is Object B, object B maintains a member attribute, and the type is Object A, and then assign values to these two member attributes separately. When assigning Object A to null, change Object B is assigned null, so that both object A and object B are null, but there is a reference relationship between a and b, so that a and b will never be recycled.
2. Mark removal method
Mark removal algorithm is to divide garbage collection into two stages, namely mark and clear
2.1 Principle
This picture represents the state of all objects during the running of the program. Their flags are all 0 (that is, unmarked, the following default 0 is unmarked, 1 is marked), assuming that the effective memory space is exhausted , JVM will stop the running of the application and start the GC thread, and then start the labeling work. According to the root search algorithm, after the labeling, the state of the object is as shown below.
As you can see, according to the root search algorithm, all objects reachable from the root object are marked as surviving objects.
The first stage marking has been completed. Next, we must perform the second phase of cleaning, then after cleaning, the remaining objects and the status of the objects are shown in the following figure.
It can be seen that the objects that have not been marked will be recovered and cleared, and the marked objects will remain, and the marked
The count is reset to 0. Needless to say, wake up the stopped program thread and let the program continue to run.
2.2. Advantages and disadvantages
It can be seen that the mark-clearing method has been solved, the circular reference problem of the reference counting method, and objects that are not referenced from the root node will be recycled.
Disadvantages:
-
The efficiency is low. Both the marking and clearing actions need to traverse all objects, and the application needs to be suspended during the GC. This experience is very poor for applications with more interactive requirements.
-
Memory fragmentation cleaned up by the mark removal method is more serious, because the objects being cleaned exist in all corners of the memory, so the cleaned memory is not coherent.
3. Mark compression algorithm
The mark compression algorithm is optimized on the basis of the mark removal method. The mark phase is the same. In the cleanup phase, instead of directly cleaning the marked objects, the live objects are compressed to one end of the memory, and then the garbage beyond the boundary is cleaned up Fragmentation problem.
3.1 Principle
3.2 Advantages and disadvantages
Solve the problem of fragmentation of mark removal method, and at the same time, the step of compression, the step of moving the memory location of the object, its efficiency has a certain impact.
4. Copy algorithm
The core of the replication algorithm is to divide the original memory space into two, only use one of them at a time, copy the objects being used to another memory space during garbage collection, and then empty the memory space, exchange two Memory roles complete garbage collection.
If there are more garbage objects in memory and fewer objects need to be copied, it is more appropriate and efficient to use this algorithm in this case, and vice versa.
4.1. Young with memory space in JVM
-
At the beginning of the GC, the object will only exist in the Eden area and the Survivor area named "From", the Survivor area "To" is empty.
-
Immediately after the GC, all living objects in the Eden area will be copied to "To", while in the "From" area, the objects that are still alive will decide where to go based on their age value. Objects whose age reaches a certain value (the age threshold can be set by -XX: MaxTenuringThreshold) will be moved to the older generation, and objects that do not reach the threshold will be copied to the "To" area.
-
After this GC, the Eden and From areas have been emptied. At this time, "From" and "To" will exchange their roles, that is, the new "To" is the "From" before the last GC, and the new "From" is the "To" before the last GC. In any case, it will ensure that the Survivor area named To is empty.
-
The GC will repeat this process until the "To" area is filled. After the "To" area is filled, all objects will be moved to the old generation.
4.2. Advantages and disadvantages
advantage:
-
In the case of many garbage objects, the efficiency is higher
-
No fragmentation of memory after cleaning
Disadvantages:
-
Not applicable in the case of few garbage objects, such as: old generation memory
-
The memory space is divided into two, only one of them is used, and the memory usage rate is low
5. Generational algorithm
A variety of recycling algorithms have been introduced above. Each algorithm has its own advantages and disadvantages. No one can replace anyone, so it is a wise choice to choose according to the characteristics of garbage collection objects. The generation algorithm is actually like this. Choose according to the characteristics of the recycling object. In the jvm, the young generation is suitable for using the replication algorithm, and the old generation is suitable for using the mark removal or mark compression algorithm.