Reference counting method
For an object A, as long as any one object references a, the reference counter of A is incremented by 1, and when the reference is invalidated, the reference counter is reduced by 1. If the value of the reference counter of the A object is 0, then object A cannot be used again.
Implementation is also very simple, only need to have an integer counter for each object.
Cons: 1. The case of a circular reference cannot be processed 2. The reference calculator requires that, with each reference generation and elimination, an addition and subtraction operation is required, which has a certain effect on the system performance.
A refers to b,b and a, so the reference counter for A and B is not 0, but there is no 3rd object in the system that references a and B. In this case, A and B are not recoverable and therefore cause a memory leak.
Attainable object: Refers to the object through which the root object is referenced and can eventually be reached. Unreachable object: A reference search through the root object, and ultimately no object to be referenced to.
Mark Clear Method
The mark clearing method is the basic idea of modern garbage collection algorithm.
The tag cleanup algorithm divides garbage collection into two phases: 1. Marking Phase 2. Purge phase
One possible implementation is to mark all objects that are accessible from the root node, first through the root node, in the tagging phase. Therefore, an object that is not marked is a garbage object that is not referenced. Then, in the purge phase, all unmarked objects are cleared.
Disadvantage: Easy to produce space debris.
The reclaimed space is discontinuous, and in the process of allocating the object's heap space, especially the memory allocation of large objects, the productivity of the discontinuous memory space is lower than the continuous space.
Replication Algorithms
Core idea: Divide the original memory space into two blocks, use only one piece at a time, and at garbage collection, copy the surviving object in the memory in use to the unused memory block, then clear all objects in the memory block in use, swap the roles of the two memory, and complete the garbage collection.
If there are many garbage objects in the system, the number of surviving objects that the replication algorithm needs to replicate will be relatively small, and the efficiency of the replication algorithm is very high when the garbage collection is really needed.
Because it is copied into the new memory space, it ensures that the reclaimed memory space is free of fragmentation. After replication, the new memory space will remain contiguous.
Disadvantage: The cost is that the system inside the passbook half.
In Java's new generation serial garbage collector, the idea of a copy algorithm is used. The Cenozoic is a three-part eden,from,to,The from/to is used to store objects that have not been reclaimed.
During garbage collection, the surviving objects in the Eden space are copied into unused survivor space (assuming to), and young objects in the survivor space (assuming from) are also copied to the to space, with large objects or older objects directly into the old age, If the to is full, the object will also go straight into the old age. At this point, the remaining objects in the Eden and from areas are garbage objects that can be emptied directly.
This improved replication algorithm not only guarantees the continuity of space, but also avoids the waste of large amount of memory space.
The replication algorithm is more suitable for the new generation.
Tag compression algorithm
Tag compression algorithm is an old-age recovery algorithm. It is a number of optimizations based on the markup cleanup algorithm.
After you mark the surviving object, compress it to one end of the memory, and then clean up all the space outside the boundary.
1. Avoid the creation of fragments 2. No need for two blocks of memory space
Very good value for money.
The same as when the tag cleanup algorithm executes, a memory defragmentation is done again.
Split-generation algorithm
The memory interval is divided into several pieces according to the characteristics of the object, and the different recovery algorithms are applied to improve the efficiency of the recovery according to the characteristics of each block of memory space.
New generation: The old age of replication: marker compression algorithm or marker cleanup algorithm
Partitioning algorithm
The entire heap space is divided into successive different communities, each of which is used independently and recycled independently.
The advantage is that you can control how many small intervals are recycled at a time.
In general, under the same conditions, the larger the heap space, the longer the time required for a GC, and the longer the pauses, so that each interval is reasonably recycled, rather than the entire heap space, based on the target's pause time, thus reducing the pause generated by a GC.
A common garbage collection algorithm