Garbage collection algorithm garbage collection algorithm is divided into garbage collection algorithm and garbage collection algorithm 1, garbage collection algorithm: 1), reference counter algorithm: there is a reference counter +1, reference destruction, counter -1, when the reference counter =0, the object has no reference, can be recycled! Disadvantages: Mutual references form a loop, can not be recycled. 2) The root search algorithm (root node algorithm, reachability analysis algorithm) starts from a root node and looks for reference nodes. The remaining nodes that are not referenced are useless and can be recycled. A), strong reference: create an object and assign this object to a reference variable, there is a reference variable pointing to will never be garbage collection; Example: Object obj = new Object(); B), soft reference: non-mandatory reference, memory overflow before recycling; Soft reference main users to achieve functions similar to caching; Example: Object obj = new Object(); SoftReference sf = new SoftReference(obj); Sf is a soft reference to obj c), weak reference: when a JVM does garbage collection, objects associated with weak references are collected regardless of whether memory is sufficient; D), virtual reference (ghost/phantom reference) : each garbage collection will be returned, mainly used to check whether the object has been deleted from memory. It is returned each time the garbage collection is performed to check whether the object has been removed from memory.

Garbage collection algorithm: 1) Mark-clean algorithm: scans from the root collection, marks the surviving objects, and then scans the unmarked objects in the whole space after marking. Disadvantages: Since the non-surviving objects are directly recycled, memory fragmentation will be caused. Scanning from the root collection and copying living objects into a new, unused space Disadvantages: a memory swap space is required for moving objects, which is expensive. 3) Mark-collation algorithm: mark objects in the same way as the mark-clear algorithm, but the clearing is different. After recovering the space occupied by non-viable objects, all viable objects will be moved to the left free space and the corresponding pointer will be updated; 4) Generation collection algorithm: according to the life cycle of the object, the memory is divided into several different regions, generally divided into the new generation (copy algorithm) and the old era (mark collation algorithm).

Serial garbage collector Serial garbage collector Serial garbage collector While garbage collection is in progress, all other worker threads must be suspended until the collection is complete. 2. ParNew Collector: A multithreaded version of Serial collector that performs better than Serial on multi-core CPUS. Insane. A new generation collector, a collector that uses replication algorithms and is a Parallel multithreaded collector. A manageable throughput can be achieved. It is ideal for tasks that do calculations in the background without much interaction. Serial Old collector: an older version of the Serial collector, which is also a single-threaded collector using a mark-collation algorithm. The CMS collector is an older version of the Parallel Avenge collector, which uses multiple threads and a mark-collation algorithm. Initial tagging (need to Stop The World,), concurrent tagging, re-tagging (need to Stop The World,), concurrent cleanup Advantages: concurrent collection, low pause disadvantages: very sensitive to CPU resources; Unable to handle floating garbage; 7, G1 collector: is a server-side application garbage collector, steps: initial mark, concurrent mark, final mark, filter collection advantages: Parallel and concurrency, generational collection, spatial collation (tag collation algorithm, copy algorithm), predictable pauses.

To be insane, insane, insane, insane, insane, insane, insane, insane Default garbage collector G1Copy the code