Garbage collection (garbage collection, GC)
Collection principle:
1, reference counting algorithm: Add a reference counter to the object, whenever there is a place to refer to it, the counter adds one; The problem is that the circular reference cannot be resolved.
2, the Accessibility Analysis algorithm: A series of objects called ' GC Roots ' as the starting point, like the next search, can reach the object is available, otherwise is not available.
GC roots objects include:
Objects referenced in the virtual machine stack (local variable table in the stack frame)
The object referenced by the class static property in the method area.
Objects referenced by constants in the method area
The object referenced by JNI (that is, generally speaking, the native method) in the local method stack.
Finalize method
1. If an object does not overwrite the Finalize method, it will not execute when the object is recycled;
2, if there is coverage, but this object has been executed once this method, when the object is recycled, will not be executed;
3, GC, an object is reclaimed, if it has overwrite the Finalize method, and has not been called before, not immediately destroy it, but instead moved it to the F-quene queue, and the virtual machine started a low-priority finalizer thread to execute it, but does not guarantee that
Wait for it to end.
GC algorithm:
1. Mark Sweep-Erase algorithm:
First mark the unreachable Team object, and then unified clear;
Insufficient is to produce memory fragmentation;
2. Copy algorithm:
Divide the memory by 2, and when one is running out, it will be able to copy the object to another, then empty the current object.
The advantage is high efficiency, which determines that memory utilization is low.
3. Labeling and Finishing algorithm:
Mark the Unreachable object first, then move the available memory forward, and finally clean up the object immediately afterwards.
4, Generation of collection algorithm:
Divide the memory into blocks, then choose different algorithms based on the characteristics of each block. For example: The new generation, the old age;
Safety points, safety zones:
While all GC collections are currently in place, the user thread needs to be paused, but the user thread is not able to be paused by the GC thread in every location. The current strategy is that the GC emits an interrupt model, each time the user thread reaches a safe point, reads the state and decides whether
Time out.
The above strategy cannot solve the problem, the user thread has entered the suspend or wait state (at this time not at the security point) before the interrupt signal is issued, so the security Area point concept is proposed;
Safe zone:
Refers to a region of code snippet, the reference does not change.
When a thread enters the security zone, it marks itself as having entered the security zone, which can be ignored if a GC is launched. If the thread leaves the security zone, the GC has completed enumerating the GC roots object, which thread continues execution, or waits for the enumeration to complete.
Collector:
1, the primary generation collector:
Serial: Single-threaded, and stop the WORLD,STW, that is, the GC will stop other user threads at the time, using a copy algorithm
Parnew: Multithreading, the number of threads and CPUs are the same, other and serial similar;
Parallel scavenge: Similar to parnew, the goal is to achieve a controlled throughput (throughput), which is the total amount of time the CPU uses for the user thread/cpu, but it does not care about the pause time.
2. Old Generation Collector:
Serial Old:serial's older version; the same is the use of single-threaded collectors, the use of marker-collation algorithm, another use as a CMS collector for the record;
Parallel Old:parallel Scavenge's senior class;
CMS (Concurrent Mark Sweep): Uses the tag-purge algorithm, the collector that targets the shortest payback time, and divides the recycling into 4 steps:
A, initial tag: Requires STW user thread
b, concurrency token: can be executed concurrently with the user thread, for example, there are 4 user threads, 4 CPUs This time the GC thread will also rotate the CPU with the user thread. There is no blocking, which is concurrency. Parallel refers to running concurrently.
C, re-mark: Need STW
D, concurrent cleanup: can be concurrent
G1 Collector: Currently in low pause time can be comparable with the CMS, low throughput above the performance of the more excellent.
Summary: At present, the mainstream virtual machine is the use of generational recovery mechanism;
1, the new generation and the old age can use different collectors, with each other to complete the recovery task, the virtual machine can use parameter configuration specific use of that garbage collection combination
2, the heap is divided into the new generation and the old age, the Cenozoic is divided into a Eden area, 2 Survivor District, which is usually Eden/survivor = 8, can also use parameter configuration.
3, usually the Cenozoic use an Eden, a survivor, recycling, will be able to copy the object to another survivor; if not enough, move to the old age, if not enough to start the full GC, or not enough to oom
4, large objects (the new generation can not put, or more than the set value) or long-term (4bit, the default 15 GC, or the custom) of the surviving objects are moved into the old generation;
5, the use of Android may be a variant of the CMS strategy.
Java garbage Collection and memory recycling