This is the fifth day of my participation in the More text Challenge. For details, see more text Challenge

I have learned several common garbage collection algorithms. I will take notes here

1. Mark-clear algorithm

The algorithm is divided into two stages: marking and clearing. First, the space that does not need to be reclaimed is marked, and the unmarked part is reclaimed uniformly after the end of marking. It is the most basic recycling algorithm, and the subsequent algorithms are improved on it.

The problem

  1. Waste of space (large amount of discontinuous space debris will be generated after markers are cleared)
  2. The efficiency problem

2. Replication algorithm

The memory is divided into two pieces of the same size, and only one piece is used at a time. When a garbage collection is completed, the living objects are copied to the other piece, and the space used is cleared, and so on.

3. Tag sorting algorithm

According to the characteristics of the old era proposed a marking algorithm, the marking process is still the same as the “mark-clear” algorithm, but the next step is not to reclaim the recyclable objects directly, but to make all the living objects move to one end, and then directly clean up the memory outside the end boundary

4. Generational collection algorithm

Current virtual machine garbage collection is based on a generational collection algorithm, which actually divides the memory into several blocks based on the lifetime of the object. 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 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.