Java Garbage Collector algorithm and java garbage collection Algorithm
Reference Counting Algorithm:
Python
Add a reference counter to the object. Every time a counter is referenced, the counter value is added with 1. When the reference fails, the counter value is reduced by 1; objects whose counter is 0 at any time cannot be used again.
Disadvantage: It is difficult to solve the issue of circular reference between objects.
Since each other is referenced by each other, their count is not 0, so the reference counting algorithm cannot notify the GC collector to recycle them.
Root search algorithm:
Java, C #, and Lisp use this.
Using a series of objects named "GC Roots" as the starting point, you can start to search down from these nodes. The path you search for is referred to as Reference Chain ), when an object to GC Roots is not connected by any reference chain (in graph theory, it is impossible to reach this object from GC Roots), it is proved that this object is unavailable.
Garbage collection algorithm:
1. Mark-clear Algorithm
First, all objects to be recycled are marked, and all marked objects are recycled after marking.
2. Copy Algorithms
Divide the available memory into two equal parts by capacity, and use only one of them at a time. When this memory is used up, you can copy the remaining objects to another one, and then clear the used memory space.
The memory is divided into a large Eden space and two smaller memory vor spaces. Each time Eden and the memory vor space are used. When recycling, copy the objects that are stored in the Eden and movie vor to another movie vor space at one time, and finally clear the Eden and the movie vor space used just now.
3. Mark-cleaning algorithm
Move all surviving objects to one end, and then clear the memory outside the boundary.
4. Generational Collection Algorithms
The memory is divided into several parts based on the lifecycle of the object. Use the most appropriate collection algorithm based on the characteristics of each age.
The new generation uses the replication algorithm, replacing the old generation with the "tag-clean" or "tag-sort" algorithm.
Reference Book: Zhou Zhiming deep understanding of Java virtual machines