introduce
In the JVM heap, almost all of our objects are managed, but objects are produced continuously. In this case, the JVM has a set of algorithms to clean up unwanted objects. These algorithms are called garbage collection algorithms, and the specific implementation is called garbage collector.
Mark-copy algorithm
Tags are first introduced replication algorithm, tag memory replication algorithm is first divided into two pieces with the same size, such as 1 g memory can be divided into two equal 512 m area of memory, every time using only one piece, when after an area full of garbage recycling, copies surviving object to another area, then one-time cleaning before the use of space.
Advantages: Fast speed, high efficiency
Disadvantages: Insufficient memory usage, using only half of the memory at a time
Mark-clear algorithm
Sweep algorithm and marking replication algorithm has a very significant difference, tag removal algorithm USES all memory, algorithm is divided into “tags” and “clear” stages: object can be a marker of survival, and then unified recycling all have not been labeled objects, and then someone said, that I can only mark clear object, can, in turn, of course! First, all the objects to be reclaimed are marked, and then all the marked objects are reclaimed uniformly after the completion of the marking. This algorithm is good, but there are two problems:
- Efficiency issues: If there are too many objects to be marked, it can be time-consuming
- Space problem: Marking the removal algorithm, after the removal of unnecessary objects will produce discontinuous memory space fragmentation problem, may lead to enough memory, some objects can not be stored.
Mark-collation algorithm
Sorting algorithm is a kind of marking algorithm, almost the same with “tag – clear algorithm steps”, after mark process and is not directly recycled object, but let all live objects will be moved to the side, and then clean up live objects beyond the boundaries of memory, so that reduces the memory space debris problem, of course efficiency will close.
conclusion
The current GARBAGE collection algorithm of virtual machines is generational collection, which divides the memory into several blocks according to the life cycle of objects. Generally, the Java heap is divided into the new generation and the old generation, so that we can choose the appropriate garbage collection algorithm for each generation.
For example, in the new generation, a large number of objects (nearly 99%) die per collection, so you can choose a replication algorithm that can complete each garbage collection with a small amount of object replication cost. Older objects are more likely to survive, and there is no extra space to guarantee their allocation, so we have to choose a mark-clean or mark-clean algorithm for garbage collection. Note that the mark-clear or mark-clean algorithm is more than 10 times slower than the copy algorithm.