Tag/clear Algorithm. If you have figured out this algorithm, then the last two will be a piece of cake.
Because if we want to recycle garbage during the running of a program (program that is the JAVA program we run on JVM), we must make the GC thread cooperate with the thread in the program, in order to smoothly recycle the garbage without affecting the program running..
The method is to stop the whole program (also known as stop the world) when the available memory in the heap is exhausted, and then perform two tasks, the first item is the mark, and the second item is the clear.
MARK: The process of marking is actually to traverse all GC Roots, and then mark all GC Roots reachable objects as surviving objects.
Clear: clears all objects in the heap and all unmarked objects.
That is, when the available memory is exhausted during the program running, the GC thread will be triggered and the program will be suspended, and then the surviving objects will be marked again, finally, all unmarked objects in the heap are cleared, and the program resumes running..
Their flag spaces are all 0(That is, if it is not marked, 0 is marked by default, and 1 is marked .)Effective memory space depletionThe JVM stops the application running, starts the GC thread, and starts the marking operation. According to the root search algorithm, after marking, the object status is as follows.
Its disadvantage is its low efficiency (recursion and full-heap object traversal), and the application needs to be stopped during GC, which leads to poor user experience., Especially for interactive applications. Imagine if you are playing a website, the website will be suspended for five minutes in an hour. Are you still playing?
The idle Memory cleared in this way is not consecutive.This is not hard to understand. Our dead objects are immediately appearing in every corner of the memory. After clearing them, the memory layout will naturally be messy. To cope with this, JVM has to maintain a memory idle list, which is another overhead. In addition, it is difficult to find continuous memory space when allocating array objects.