Garbage collector GC (garbage Collection)
One, reference counting algorithm (Reference counting)
Description: Add a reference counter to the object, and whenever a place references it, the data device adds 1, and when the reference fails, the counter is reduced by 1, and the counter is 0 to be recycled.
Advantages: Simple to achieve, high judgement efficiency
Disadvantage: It is difficult to solve mutual circular references between objects (obja.instance = OBJB; Objb.instance = Obja), so the Java language does not use the reference counting method to manage memory
Second, root search algorithm (GC root tracing)
Both Java and C # use Root search algorithms to determine whether an object survives. Through a series of objects named "GC Root" as starting points, the search for all traversed paths is called the reference chain (Reference Chain), and when an object is connected to GC root without any reference chain (in graph theory, GC Root to this object is unreachable), proving that the object can be recycled.
What objects in Java can be GC Root?
- Reference objects in the virtual machine stack (local variable table in the stack frame)
- Object referenced by class static property in the method area
- Constants in the method area reference objects
- Reference object for JNI (that is, the native method) in the local method stack
Third, Mark-Clear Algorithm (Mark-sweep)
The objects that need to be recycled are marked first, and all tagged objects are collected uniformly after the tag is complete.
Cons: Efficiency issues and space issues (marked cleanup creates a lot of discontinuous memory fragmentation, too much memory fragmentation can cause the program to allocate large objects when it cannot find enough contiguous memory space to have to trigger another garbage collection action earlier)
Iv. replication Algorithm (Copying)
Divide the memory into two blocks of equal size, using only one piece at a time. When this memory is used up, the surviving objects are copied to another piece of memory, and the used memory space is cleaned out once.
Pros: GC Only one piece at a time, regardless of the memory fragmentation problem, and simple, efficient operation
Cons: Memory shrinks by half
Note: Today's commercial virtual machines are using this collection algorithm to reclaim the new generation. The memory is divided into a larger Eden space and two smaller survior spaces, Each time you use Eden and one of the survior. When recycled, the objects that are still alive in Eden and Survior are copied once to another Survior space, and the survior space that was used in Eden and just before is cleared.
Five, marking-finishing algorithm (mark-compact)
Let all surviving objects move toward one end, and then clean all memory outside the end boundary directly.
Six, generational collection algorithm (generational Collection)
The memory is divided into several blocks according to the life cycle of the object, which is divided into the new generation and the old age, and adopts different collecting algorithms according to the characteristics of each age. New generation (small survival) replication algorithm, the old age (high object survival rate) "Mark-clean" algorithm
Supplemental: Partition Memory Introduction
Total JVM memory is divided into three generations: young Generation, Old Generation, persistent (Permanent Generation)
1, Young generation: All newly generated objects are first placed in the young generation of memory. The goal of the young generation is to remove the short-life-cycle objects as fast as possible. The young generation of memory is divided into a larger Eden space and two smaller survior spaces, Each time you use Eden and one of the survior. When recycled, the objects that are still alive in Eden and Survior are copied once to another Survior space, and the survior space that was used in Eden and just before is cleared.
2, old age: After the young generation has experienced n GC, the object that still survives, will be placed in the old age. As a result, the older generations are considered to be those that have long life-cycle objects.
3, Persistent generation: Basic fixed, for storing static files, such as Java classes and methods. The persistence generation has no significant effect on the GC. Persistent generations can be set through-xx:maxpermsize=<n>.
Java Garbage Collection Algorithm introduction