GC can be divided into two main steps:
(1) Garbage confirmation
(2) Garbage collection algorithm
1. Validation of garbage objects:
(1) Reference counting method: add a counter to the object. Each time the object is referenced, the counter count increases by 1. When the reference is invalid, the counter count decreases by 1.
In Java, objects are referred to each other in a loop, and the counter cannot be 0, so it cannot be recognized as a garbage object.
(2) Reachability analysis: through a series of objects called GC Roots as the starting point, search step by step from the corresponding node downward, the search path is called the Reference Chain. When there is no Reference Chain between an object and GC Roots, the object is unavailable and confirmed as a garbage object
2. Garbage collection algorithm:
(1) Mark-clearing algorithm:
1) Mark all active objects
2) Clear all inactive objects
There are other explanations that have emerged: 1) mark all objects that need to be reclaimed and 2) clear marked objectsCopy the code
Main idea: Clean up inactive objects, but the implementation is different.
Advantages: More efficient in the case of living objects
Applicable to old generation (i.e., old generation)
Disadvantages: memory fragmentation and garbage collection can be triggered early when a large object (typically larger than each chunk in the free table but less than the sum of two) is added
The entire space is scanned twice (the first time: mark the living object; Second: Clear unmarked objects)
(2) Replication algorithm: the memory area is divided into two areas, and only one of them is used each time. When the memory of the region is fully used, the surviving objects are copied to another region, and the objects in the current region are completely recycled.
Advantages: More efficient in the case of fewer living objects
Scans the entire space once (marks live objects and copies move)
Applies to the younger generation: basically 98% of the objects are “live and die”, few will survive
Cons: Requires an empty chunk of memory
Move objects need to be copied
(3) Mark-collation algorithm: similar to mark-clean, but the next step is not to clean up the recyclable objects directly, but to move all the surviving objects to one end, and then directly clean up the area beyond the end boundary
Advantages: This method not only avoids fragmentation, but also does not require two identical memory Spaces, so it is cost-effective
(4) Generational algorithm: Generational collection is the algorithm commonly used by JVM at present, that is, the new generation adopts the replication algorithm, because a large number of new objects die and only a few survive; Mark-collation is used in the old age because the object has a high survival rate and there is no extra space to guarantee it