Java garbage collection algorithm and Detailed

First, let's Zhang ancestral mind map:

 

  Memory recycling and garbage collectors are in many cases affect system performance, concurrency is a major factor, the reason why the virtual machine offers a variety of collectors and provide a large number of adjustable parameters, because only based on application requirements, implementation of selection the best way to get the best garbage collection performance.

GC happened there:

  Virtual Machine JVM runtime memory area is mainly divided into (below): a virtual machine stack, native method stacks, program counter, Java heap, the method area. Where the virtual machine stack, native method stacks, program counter for the thread private area, in these areas do not need too much to consider the issue of garbage collection, since the end of the method or the end of the threads, memory recovered naturally follow. The Java heap and method area is not the same, the two regions share the thread area, a plurality of interface implementation class memory required may be different, multiple branches need a method of memory may not be the same, we known only created when the program is run during those objects, the two parts of memory allocation and recovery are dynamic, garbage collection occurs mainly in two parts of the region. JVM memory area analysis can refer to: xxxxxx

How to determine those Java objects need to be recovered:

  Now the mainstream virtual machine is using reachability analysis algorithm to determine whether an object can be recycled, instead of using reference counting algorithm to determine the parallel secondary screening to mark the final recycle objects.

1, reference counting algorithm:

  The basic idea: In addition to the subject a reference counter, whenever a reference to its place, the counter value by 1; when referring to the failure, the counter value by one; counter is zero at any time when the object is not being used, a recyclable .

  Examples: t1, t2 whether the object will be recycled when the gc happen? If according to another reference counting algorithm reference objects between t1 and T2, the object can not be recovered, but the result is to be recycled. DESCRIPTION virtual machine not use reference counting algorithm to determine whether an object can be recovered. The main reason is the reference counting algorithm to solve the difficult problem of mutual references between objects.

 1 public class TestGC {
 2     public Object instance = null;
 3     private static final int oneMb = 1024 * 1024;
 4     private byte[] bigSize = new byte[2 * oneMb];
 5     
 6     public static void test(){
 7         TestGC t1 = new TestGC();
 8         TestGC t2 = new TestGC();
 9         t1.instance = t2;
10         = t2.instance T1;
 . 11          
12 is          T1 = null ;
 13 is          t2 = null ;
 14          // here GC occurred, t1 and t2 can be recovered if? 
15          System.gc ();
 16      }
 . 17 }

2, reachability analysis algorithm:

  When a series is referred to as "GC Roots" object as a starting point, to start the search downward from these nodes, called search path traversed chain of references, when an object is not connected to any reference GC Roots chain: The basic idea , it indicates that the object is not available, can be recovered.

Can be used as an object GC Roots include:

  (1) the object is referenced in the virtual machine stack

  Class (2) Method static property reference object region

  (3) The method of constant reference object region

  (4) JNI native method stack referenced objects

  It can be understood as:  

  (1) The first is a first object reference of the virtual machine stack, we create an object in the normal procedure, the object will open up a space on the heap, and will address this as a reference space to save the virtual machine stack , if the object life cycle is over, it will be referenced from the virtual machine stack stack, so if there is a reference in the virtual machine stack, it means the object is useful, this is the most common.

  (2) The second is that we define a global static object in the class, that is, the use of the static keyword, because the virtual machine thread stack is private, so the reference to such an object will be stored in the shared approach area obviously the method of static reference area as GC Roots is a must.

  (3) The third is the constant reference is to use the keyword static final, because the initialization after this reference is not modified, so the constant pool area object method references should be considered as GC Roots. The last one is in the use of JNI technology, sometimes pure Java code does not meet our needs, we may need to call C or C ++ code in Java, and therefore the use of native method, JVM memory, there is a special local method stack to save a reference to these objects, so the object native method stacks will be referenced as GC Roots.

  Whether it is by reference counting algorithm or reachability analysis algorithm determination object determines whether the object is alive and all related references cited java includes four species (from high to low): strong reference (Strong Reference), soft references (Soft Reference ), weak reference (weak reference), the virtual reference (Phantom reference).

  • A strong reference (StrongReference)
    strong reference is the most commonly used reference. If an object has a strong reference, and that the garbage collector will never recover it. When the memory space is insufficient, Java Virtual Machine would rather throw OutOfMemoryError errors, abnormal termination of the program, it will not recover by random objects that have strong references to solve the problem of insufficient memory. Similarly Object obj = new Object ().
  • Soft references (SoftReference)
    If an object has only soft references, the memory space is sufficient, the garbage collector does not reclaim it; if the memory space is insufficient, it will reclaim the memory of these objects. As long as the garbage collector does not reclaim it, the object can be used by the program. Soft references used to implement memory-sensitive cache (example given below).
    Soft references can be a reference queue (ReferenceQueue) used in combination, if the soft reference object referenced by the garbage collector, Java virtual machine will be added to this soft references cited associated queue.
  • Weak references (WeakReference)
    weak references and soft references that difference: only a weak reference objects have more short life cycle. In the process of the garbage collector thread scans memory area under its jurisdiction, once the objects found only a weak reference, regardless of the current memory space is sufficient or not, will reclaim its memory. However, the garbage collector is a low priority thread, and therefore will not necessarily quickly find objects that have only a weak reference.
    Weak references can queue and a reference (the ReferenceQueue) used in combination, if the object referenced by the weak reference is garbage, Java virtual machine will be added to this reference weak reference associated queue.
  • Virtual reference (PhantomReference)
    "Virtual Reference" by definition, is non-existent, and several other references are different, the life cycle of virtual reference and does not determine object. If an object holds only a phantom reference, and then it does not have any references, they are likely to be garbage collected at any time.
    Phantom reference primarily used to track objects recovered garbage collector activity. With reference to a virtual reference difference between the soft and weak reference is that: the virtual reference and reference must queue (the ReferenceQueue) used in combination. When the garbage collector is ready recovery of an object, it is found that if there is a virtual reference, will be recovered before the memory, this reference is added to the virtual queue associated with a reference.

3, the final determination (secondary Markers):  

  Even after the object has been determined that the reachability analysis algorithm, found no reference to the GC Roots up the chain, this object will not be recovered immediately, also we need to go through secondary screening process marks, in order to recover the object.

  (1) marked the first time: If an object is not covered finalize () method, or a finalize () method has been executed virtual machine, then both cases, the virtual objects deemed it "unnecessary to perform" objects can be recycled.

  (2) Second marking: the first marker to the opposite case, if the object is determined to be necessary to perform a finalize () method, then the F-Queue object into the queue, after automatically establishing a virtual low-priority level Finalizer thread F-Queue objects in the secondary mark, if an object finalize () method successfully re-associated with any reference to an object on the chain, it will be removed from the object "will recover" collection the object is not recovered, or they will be recovered. Each object has a finalize () method (Object class defines the method), finalize () method is the last chance to escape the object to be recovered.

How to recycle garbage JVM memory:

  Of course, using all kinds of garbage garbage collection algorithm to clean up memory, including: mark - sweep algorithm (Mark-Sweep), replication algorithm (Copying), Mark - Collation Algorithm (Mark-Compact), generational collection algorithm (Generational Collection).

  1, mark - sweep algorithm (Mark-Sweep):

  The basic idea: First all need to be recovered objects, unifying all objects are marked recovery after labeling is completed, its labeling process is based on reachability analysis algorithm and process to determine the marks and secondary marks.

  Disadvantages: (1) mark and sweep the two processes is not efficient; will produce a large number of discrete memory chips (2) mark clear; too much memory fragmentation can cause later when the program is run needs to allocate large objects, can not find enough contiguous memory space and have been forced to trigger another garbage collection action.

 

   2, replication algorithm (Copying):

  The basic idea: it available memory is divided into two equal size, uses only one of them. When this one runs out, it will also live objects copied to another one above, then clean out once in the memory space has been used. Such that each time a block of memory which is recovered, the case does not produce debris, as long as the mobile set stack pointer in order to allocate memory, simple, efficient operation.

  Copy the new generation algorithm mainly used in recovery, new generation memory is divided into: areas Eden, From Servivor zone, To Servivor region, as shown:

 

   Disadvantages: (1) memory is reduced to half. (2) If the object requires a higher survival rate than the copy operation a plurality of times, the efficiency decreases. (3) the need for additional space allocated guarantee.

  3, Mark - Collation Algorithm (Mark-Compact):

  The basic idea: tagging process and marks - the same sort algorithm, but not directly on the next steps to clean up the object recyclable, but to all surviving objects are moved to the end, then clean out the memory directly outside the terminal boundary.

 

   4, generational collection algorithm (Generational Collection):

  The basic idea: Depending on the survival time of the object, the java heap into the new generation and the old era, the most appropriate collection algorithm based on the characteristics of each era. In the new generation, each garbage are found to have died when a large number of objects to collect, only a few survived, the algorithm can be used to copy, simply copy the target a small number survive to complete the collection. In the old era of high survival rate of the object, there is no guarantee extra space is allocated, you can use "mark - sweep" or "mark - finishing" algorithm.

The garbage collector

  As shown below, java garbage collector mainly about seven kinds, acting on the new generation: Serial collector, ParNew collector, Parallel Scavenge collector. The role of the old year: Serial Old collectors, Parallel Old collectors, CMS (Concurrent Mark Sweep) collector. G1 (Garbage First) collectors mainly for the garbage collector server applications. Connection exists between the two collectors, indicating that they may be used together with.    

 

 

Unfinished, continued ...  

 

Reference article:

  "In-depth understanding of java virtual machine" Zhou Zhiming

Note: This article is studying "in-depth understanding of java virtual machine," the study notes a book as a way to sum up and learn. This picture comes from the most network intrusion deleted.

 

Guess you like

Origin www.cnblogs.com/oush/p/11686671.html