The GC solves three major problems:
- What memory needs to be recycled?
- When do I recycle?
- How to recycle?
What memory needs to be recycled?
The simplest is the reference count (reference count), and the flaw is that the circular reference cannot be resolved. The faster algorithm is the accessibility analysis (reachable).
When do I recycle?
At least two marking processes to go through:
- After the accessibility analysis, it was found that no reference chain connected to GC roots was first flagged and filtered--whether it was necessary to perform finalize ().
- Object does not have a finalize () method or is executed by the virtual machine, as "No need to execute"
- It is necessary to perform finalize (), be placed in the F-queue queue, and then "execute" (triggering the corresponding method, but not waiting for it to be completed) at a later time by the finalizer thread (the virtual machine automatically establishes a low priority).
- The GC then marks the second small size of the objects in the F-queue.
How to recycle?
- Mark-sweep (Mark-Clear): (defect: Causes a large amount of memory fragmentation.) )
- Copy: The memory is divided into two, using only half of them at a time. When you clean up, copy the surviving object to the unused half, and then clean up the half that is used. (Defect: Reduces memory to half the original, and is inefficient in the case of many surviving objects.) So this algorithm is used in the Cenozoic region, a chunk of Eden and two blocks of survivor area. Each time you use Eden and one of the survivor areas. )
- Mark-compact (Marker-collation): Let all surviving objects move toward one end, suitable for areas with more inventory objects, such as the Laosheng generation.
- Generational collection: All of the above methods are combined to use different algorithms for different generation regions.
Reference Documents :
- Java garbage Collection (GC) mechanism detailed
- "Thinking in Java"
- "In-depth understanding of Java virtual machines"
Java Garbage Collection Overview