“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

What is a tag clearing algorithm?

The mark clearing algorithm is a direct total stop algorithm. Simply put, they find all unreachable objects and put them in the Free list, Free.

The running process of the tag clearing algorithm

The cleaning process will be divided into the marking phase and the cleaning phase.

  • Mark phase
    • Walk through all the objects looking for the mark active object
  • The sweep phase iterates through all objects to remove unmarked objects, and at the same time it clears the marks made in the first phase so that normal work can be carried out later.

graphic

As shown in the figure, when used in our global, we recurse the following attributes and add the corresponding tags to the reachable objects, while A1 and B1 are not used in global and will be cleared in the next round

Advantages and disadvantages of the tag clearing algorithm

advantages

Is the so-called nobody is perfect, so is the algorithm no algorithm is 100% perfect, there are good and bad, but people are compared out! A very, very big advantage over the reference algorithm is that it can recycle the values of objects that are referenced to each other. Ha ha ha ha ha!

disadvantages

As shown in the figure, we simulate the memory storage situation. The red area is our reachable object, and the reachable object has two unreachable areas. In this case, during the clearing operation, the left and right areas will be cleared, and the space will be put into the free space, and then the content will be allocated to the object.

Space fragmentation The memory cleared out in this way is discontinuous, resulting in memory fragmentation and the need to maintain a free list. For example, we have only reclaimed 3 nodes, but the use of space is not normal and perfect because of the disconnection

In a nutshell

  • The efficiency is not high
  • During GC, the entire application needs to be stopped, resulting in a poor user experience
  • The memory cleared in this way is discontinuous, resulting in memory fragmentation and the need to maintain a free list.