Reference counting algorithm:
Python uses the
Add a reference counter to the object, and whenever there is a reference to it, the counter value is incremented by 1, and when the reference fails, the counter value is reduced by 1, and any object with counter 0 at any time is impossible to use again.
Disadvantage: It is difficult to solve the problem of mutual circular references between objects.
Referencing each other, causing their counts to be no more than 0, the reference counting algorithm cannot tell the GC collector to reclaim them.
root search algorithm:
That's what Java,c#,lisp used.
Through a series of objects named "GC Roots" as the starting point, starting from these nodes, the search path is called the reference chain (Reference Chain), when an object to the GC Roots no reference chain connected (in the words of graph theory is from the GC Roots to this object is not available, it proves that this object is not available.
garbage Collection algorithm:
1. Mark-Sweep algorithm
All objects that need to be reclaimed are marked first, and all tagged objects are collected uniformly after the tag is complete.
2. Copy Algorithm
Divide available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is run out, copy the surviving object to the other piece, and then clean up the used memory space once
Divide the memory into a larger Eden space and two smaller survivor spaces, each using Eden and its survivor. When recovering, copy objects that are still alive in Eden and survivor to another survivor space, and finally clean up Eden and the survivor space just used
3. Mark-Sweep algorithm
Let all surviving objects move toward one end, and then clean out the memory outside the boundary directly
4, generational collection algorithm
Divides the memory into several blocks depending on the life cycle of the object. The most appropriate collection algorithm is used according to the characteristics of each age.
The new generation uses the replication algorithm, the old age with the "mark-clean" or "mark-tidy" algorithm.
Reference book: Zhou Zhiming "in-depth understanding of Java virtual machines"
Java Garbage collector algorithm