In reading "Thinking in Java", simply record the type and rationale of the garbage collection mechanism. 1, reference count each object has a reference counter, when the reference is connected to the object, reference counter +1, when the reference leaves the scope or null, reference counter-1. When the counter is 0 o'clock, the space occupied by the object is freed. Flaw: If an object has a circular reference, "the object should be recycled, but the counter is not 0". 2. Stop-Copy (stop and copy) pauses the running program (not in background recycle mode), copies all surviving objects from the current heap to the other heap, and is not replicated is garbage. When an object is moved to another heap, they are next to each other and can simply allocate new space, and all references to those objects are corrected. 3. Mark-Sweep (Mark and sweep) iterates through all references to find all surviving objects. If the object survives, a tag is set. The cleanup action will not begin until all markers are complete. The garbage collector periodically conducts full cleanup actions, in which Java virtual opportunities are monitored, and if the object state is stable, the garbage collector becomes inefficient and switches to tag-sweep mode. If there is a lot of fragmentation in the heap space, it switches to stop-copy mode. This is the adaptive feature of the garbage collector. The Java Virtual machine can also use the JIT compiler technology, you can translate all or part of the program into machine code, improve the speed of the program.
Garbage collection mechanism