Determine which objects can be recycled
GC is based on whether the object survives to determine whether or not to recycle, there are two main algorithms to determine whether the object is alive: Reference counting algorithm, accessibility analysis algorithm
Reference counting algorithm
The principle of reference counting is to add a reference counter to the object, each quoted counter plus 1, the reference fails minus 1, when the counter 0 indicates that the object is not referenced, Can be recycled, the reference counting method is simple and efficient, but there is a circular reference problem between objects, which may lead to the inability to be recycled by GC , and it takes a lot of effort to solve circular reference problems
Accessibility analysis algorithm
The algorithm principle of accessibility analysis is to start by traversing the object root reference (stack, static reference of method table and constant reference area, local method stack) to search all reachable objects, to form a chain of references, to tag both reachable objects and unreachable objects at the same time, to indicate that no references exist, and can be GC Recycling
In general, what we call GC is in the new generation and the old age, the new generation of objects survival time is short, low survival rate is generally used to stop - copy algorithm, the old age object survival time is long, high survival rate, the general use of markers - Organize, Mark - clear algorithms, specifically what algorithm to use and the specific garbage collector
Typically new, for the JVM, where the object is placed in the Cenozoic, after the Minor GC continues to survive, the Minor GC will move all live objects in Eden to the Survivor area, and tag, if the number of tokens reaches 15 (the default), the object remains alive, Then the object will be put into the Laosheng generation.
When doing minor GC, only the new generation is recycled, will not be recycled in the old age. Even if the object of the old age is not indexed, it will still survive until the next full GC (Major GC).
GC Memory Recovery mechanism