JVM-5-GC (Garbage Collection) garbage collection

GC (Garbage Collection) garbage collection
 
What is garbage collection
Garbage collection is a dynamic storage management technology, which automatically release the object is no longer referenced by the program to implement automatic recovery of resources in accordance with the specific function of garbage collection algorithms. When an object is no longer referenced memory reclaim the space it occupied, so the space was later to use the new object to avoid memory leaks.
 
 
Memory is no longer being used to space garbage
 
c garbage collection is artificial, heavy workload, but high controllability.
java is automated, but the controllability is poor, sometimes even memory overflow occurs,
Memory overflow of objects in memory that is allocated jvm excessive, exceeds the maximum size of allocated memory.
 
System.gc () is used to invoke the garbage collector, when you call, the garbage collector runs to reclaim unused memory space. It will try to release the memory occupied by the object to be discarded.
However System.gc () call only statement is no guarantee that calls to the garbage collector.
So System.gc () can not be said to be perfect initiative to garbage collection.
 
 
A jvm how to determine which objects should be recovered
 
Two classic algorithm whether objects are recycled: Reference counting, reachability analysis algorithm.
 
Reference counting
JVM maintains a reference count for each object, the object A reference count of zero is assumed that there are no tasks described object references the object A, the object A that can be recycled, but the reference count has a drawback that can not be solved circular references.
 
Reachability analysis algorithm
In order to resolve the reference cycle counting method reference problem, Java method using reachability analysis.
GC Roots through a series of GC Roots object named as a starting point, to start the search downward from the nodes, called a reference path searched chain, when an object has no references to GC Roots chain joined, it is proved that the object unavailable.
In Java, an object can be used as the GC Roots of the following categories:
* Virtual Machine stack referenced objects;
* Method zone class static object attribute reference;
* The method of constant region of an object reference;
* Native method stacks in the JNI object references (i.e., the general said Native method);
 
When an object is, the whole time through GC Roots not search, indicating that the object can be recovered, but when the recovery depends on GC mood!
 
After determining the two in which objects can be recycled, jvm will be recycled at what time
 
1 will be recovered automatically in idle cpu time
After 2 storage in heap memory is full
3 initiative to call System.gc () attempts to recover
 
 
 
 

Guess you like

Origin www.cnblogs.com/wf-zhang/p/11965848.html