JVM garbage collection algorithm and jvm garbage collection Algorithm
Before talking about the garbage collection algorithm, let's talk about how JVM determines which objects are "garbage ".
1. Reference Counter algorithm:
The counter reference algorithm sets a counter for each object. When this object is referenced somewhere, counter + 1. When the reference fails, counter-1, when the counter is 0, the JVM considers that the object is no longer used and is "junk.
Counter reference is simple and efficient, but it cannot solve the problem of circular reference (Object A references object B, object B references object A again, but object, object B is no longer referenced by any other object. At the same time, the increase or decrease of counter brings a lot of extra overhead. Therefore, after JDK1.1, this algorithm is no longer used.
2. root search method:
The root search method uses some "GC Roots" objects as the starting point to start searching from these nodes, and the paths used for searching become Reference chains ), when an object is not connected by the reference chain of GC Roots, this object is unavailable.
GC Roots objects include:
A) Objects referenced in the VM stack (the local variable table in the stack frame.
B) Objects referenced by class static attributes in the method area.
C) Objects referenced by constants in the method area.
D) Objects referenced by JNI (Native method) in the local method stack.
After learning how the JVM determines that the object is "junk", let's start with the question. Let's look at the garbage collection algorithm.
1. Mark-clearing algorithm (Mark-Sweep)
The mark-clearing algorithm has two phases: "mark" and "clear ". In the tag phase, identify all objects to be recycled and mark them. The purge phase follows the markup phase to clear unavailable objects.
The mark-clearing algorithm is the basic collection algorithm. The efficiency of the Mark-clearing phase is not high, and a large amount of discontinuous space is generated after the Mark-clearing phase is cleared. In this way, when the program needs to allocate large memory objects, you may not be able to find enough continuous space.
Before garbage collection:
After garbage collection:
Green: Active Object RED: recyclable object White: unused space
2. Copying)
The replication algorithm divides the memory into two equal parts. Each time one of them is used, when garbage collection is performed, the surviving objects are copied to the other, and the entire memory is cleared.
The replication algorithm is easy to implement and runs efficiently. However, because only half of the algorithm can be used at a time, the memory usage is not high. The current JVM uses the replication method to collect the New Generation. Most of the objects (98%) in the new generation are killed overnight, so the ratio of the two memory blocks is not (about ).
Before garbage collection:
After garbage collection:
Green: Active Object RED: recyclable object White: unused space
3. Mark-Compact)
The markup-sorting algorithm is the same as the markup-clearing algorithm, but the markup-sorting algorithm does not copy a surviving object to another memory, but moves the surviving object to one end of the memory, then, the memory outside the boundary is recycled directly.
The tag-sorting algorithm improves the memory utilization and is suitable for the old age when the collection object remains alive for a long time.
Before garbage collection:
After garbage collection:
Green: Active Object RED: recyclable object White: unused space
4. Generational Collection)
Generation-based collection divides the memory into new and old generations based on the survival time of objects. Based on the Survival characteristics of generation objects, each generation uses different garbage collection algorithms. The new generation uses the tag-copy algorithm, and the old generation uses the tag-sort algorithm.
The implementation of spam algorithms involves a lot of program details, and the implementation methods of different virtual machine platforms are also different. The above is just a basic idea.
Article