the Java garbage collection principle
Today, read the Java garbage collection principle, feel the step-by-step understanding process is very interesting, it is recorded for later use. The main points of the following aspects of understanding: the most understandable explanation of the basic principles of improving efficiency of the method to reduce the cost of additional methods to further reduce the cost of the adaptive method of the most convenient understanding of the rationale for the explanation
Garbage collection is a collection of memory space occupied by objects that are no longer referenced within the heap. So the judge is not rubbish, is to determine whether there are references to the object.
So if you have a counter for each object, to record the number of references on it, each additional reference, counter plus 1, each reduction of a reference, the counter minus 1 (down to 0), in the recovery, traversing all objects, the counter to 0 of the memory of the object can be recycled.
This is very easy to understand, but it is inefficient to traverse all objects in the heap, there is also a fatal problem, if there is a circular reference between the objects, when the external reference to their disappearance, they should be recycled, but their counters are not 0, which has been unable to reclaim the memory, easily lead to memory leaks. improved methods of improving efficiency
Starting from a reference in the stack, locate the object it references, then find the reference that the object contains, iterate through all the references, and find all the objects that are not garbage, copy them into the new heap, update their references to the memory address of the new heap, and finally recycle the old heap. This does not appear to be an issue where circular references cannot be recycled, and the addresses in the replicated heap are contiguous and do not appear fragmented.
This method is called stop-copy, and the program pauses when the recycle occurs. Stop-Replication is much more efficient, but its maximum overhead is the process of replication. additional ways to reduce overhead
Memory allocations are in blocks, some objects are large, a single object takes up a block, and if you replicate such large objects, the overhead is large and meaningless.
To avoid meaningless copying of blocks, mark each block, mark whether it is in use, traverse the reference, and if a small object is found, copy to the new block, and then defragment the new block after the recycle is complete. If a large object is found, the block is marked and no duplication is required, which reduces the overhead of replication. an adaptive approach to further reduce overhead
If there is not much garbage in memory, a stop-and-copy will consume a lot of resources in vain. In order to avoid the stop-copying in the absence of rubbish, there is also a method called "Mark-sweep". When traversing a reference discovery object, set a tag on the object, and when the traversal ends, the object that is not marked is garbage and is cleaned up.
This cleanup will not replicate, as a result, the cleaned area will be fragmented and can only be used when litter is scarce or not, so it should be used in conjunction with stop-and-copy, marking-sweeping when objects are more stable, and stopping-copying when large fragments are found, which is adaptive technology.