Garbage collection
Without garbage collection, memory will be consumed sooner or later because we are constantly allocating memory space without recycling. Unless the memory is infinitely large, we can be self-willed and not recycled, but that is not the case.
Garbage collection actually did two things: 1, found useless objects; 2. Reclaim memory space occupied by useless objects.
Garbage collection algorithm
1. Application Counting method
Object, a counter value of +1 is added whenever a reference to the object is referenced, and the counter value is 1 when the reference is invalidated. Any object with a count value of 0 at any time is impossible to be used again. This algorithm uses a lot of scenarios, but the algorithm is not used in Java because it is difficult to solve the case of mutual reference between objects.
2, the application can reach the law
By a series of objects called "GC Roots" as starting points, searching down from these nodes, the path traversed by the search is called the reference chain, and when an object to the GC Roots does not have any reference chain (that is, the GC Roots to the object is unreachable), it proves that the object is not available.
Generation of garbage collection
"Java Starter series" garbage collection