1. Java's garbage collection mechanism works mainly on determining which objects are garbage, reclaiming the space occupied by useless objects, and keeping the surviving objects in the heap tightly aligned.
2. Java Garbage collection algorithm:
Reference count (basic not used):
When a reference is directed to an object, the object's reference counter is +1;
The reference counter-1 when the reference leaves the object waving marked null;
When the reference counter is 0 o'clock, the space occupied by the object is freed.
Flaw: If there is a circular reference between objects, the object may not be recycled.
Stop-copy:
Object discovery:
All references are traversed from the stack and from the static store, the referenced object is found, and all references contained by the object are found to find all the living objects.
Processing:
Pauses the program to run, copying live objects from one heap to another;
Objects that are not copied are garbage, the copied objects are arranged on the new heap, and references to the copied objects need to be fixed;
Disadvantages:
Requires more space (two heaps);
For the stable state of the program, may only produce very little garbage, this time "stop-copy" the comparison of waste time;
Marking--Cleaning:
Object discovery:
The process is similar to "stop copying", which sets the tag for the surviving object during traversal;
Processing:
Objects that are not marked are garbage, recycled, not duplicated, and may cause memory discontinuities.
Generational garbage collection (based on the life cycle of the object, with different garbage collection algorithms in different regions):
Young objects are placed in young areas, and objects in this area face more frequent garbage collection;
After several garbage collection, the target of the young region will be put into the old area, and the frequency of garbage collection is relatively low.
When there is insufficient space in the old area, a global garbage collection is triggered.
Information on generational recycling:http://developer.51cto.com/art/201201/311696.htm
Java garbage collection mechanism