How can I determine whether an object is alive during JAVA garbage collection? One method is reference counting, but this method cannot solve the problem of circular reference. The other method is root set search, which searches for all reachable objects from the root set, the content of the root set includes all references in the stack, static attribute references and constant references in the method area, and so on.
Garbage collection algorithms include:
Mark clearing algorithm: leaves memory fragments;
Copy algorithm: You need to divide the Eden space and vor space. When clearing the Eden space, you need to copy the surviving objects to the same vor space. In case of insufficient vor space, you need to be guaranteed by the old age;
Mark Sorting Algorithm: similar to mark clearing, but the surviving object is sorted into a space segment, and the rest end is idle;
Generational collection: the new generation uses the replication algorithm and the old generation uses the token truth algorithm;
Garbage Collector:
New Generation of Serial, replication algorithm, single-thread garbage collection, stop user thread execution during collection;
ParNew generation, replication algorithm, multi-thread parallel garbage collection, stop user thread execution during collection;
Parallel Scanvenge new generation, replication algorithm, multi-thread Parallel garbage collection, stop user thread execution, achieve controllable throughput (throughput optimization );
In the Old age of Serial, single thread, marking sorting algorithm, stopping user thread execution during collection;
In the Old age of Parallel, multiple threads, marking algorithms, and stopping user thread execution during collection;
CMS, concurrent mark-swap, old generation, mark clearing algorithm, multi-thread parallel collection, concurrent execution with user threads, minimum pause of garbage collection; disadvantages: CPU is sensitive, floating garbage cannot be processed, space fragments are generated, and sometimes Full GC is triggered;
Configuration Policy for garbage collection:
Objects are preferentially allocated in Eden.
Large objects directly enter the old age;
The survival threshold of long-lived objects in the old age;
When a Minor GC occurs, it is used to predict whether the space in the old age can meet the guarantee requirements. If the guarantee requirements are not met, the worst case is Minor GC-> guarantee failure-> Full GC; if the guarantee failure is not allowed, Full GC is performed directly;