How to determine the objects can be recycled

Jvm is to determine how an object can be recycled?

Method a: Reference counting

  jvm added to the object a reference counter, whenever a reference to its place, counter is incremented when the reference failure counter is decremented, subject to any time the counter 0 is no longer being used.

  Limitations of this method: mutual references between objects can not solve the problem. E.g:

  

 

   

 

  As shown above, the reference stack and heap objects objA objB, and at this time objA objB reference counter are incremented by one; and assigned to the member variable objA objB, the member variable is assigned to objB objA, that objA objB exists between the reference and, at this time, and objA objB reference counter becomes 1 2 are coupled. And when the stack of objA objB and references fails, and this time objA objB actually useless objects, but objA objB reference counter and also is not yet only 1 minus 0, and thus objA two objB the object will not be recovered. However, any occupation heap memory. Such a situation more on the memory leak problem that may arise.

Method Two: reachability analysis algorithm

  Through a series of objects referred to as "GC Roots" as a starting point, to start the search downward from these nodes, called search path traversed reference chain (Reference Chain), when an object is not connected to any reference GC Roots chain (in the words of graph theory, it is from the GC Roots to this object is unreachable), then it proves that this object is not available.

  

 

   

  In the Java language, as an object GC Roots may include the following categories:

    Virtual Machine stack (Local Variable Table stack frame) in the object reference.

    Method static property class object referenced area.

    Object literal reference methods zone.

    Native method stack JNI (ie, the general said, Native Method) referenced objects

Method three:

  Common reference types:

    Strong Quote: common variable reference

    Soft Quote: Object reference type of the object wrapped with SoftReference soft, not normally be recovered , but after the release GC done found no space for new objects, the objects will be recovered out of these soft references. Soft references can be used to achieve high sensitivity is not memory cache. public static SoftReference <User> user = new SoftReference <User> (new User ());

    Weak References: Object reference type of the object wrapped with WeakReference weak, weak references with almost no references, GC will be recovered directly out , rarely used.

    public static WeakReference<User> user = new WeakReference<User>(new User());

    False quote: Virtual reference has become a ghost or a phantom references cited, it is the weakest kind of reference, almost without.

Method four: finalize () method ultimately determine whether the object is alive

  Even unreachable in reachability analysis algorithm objects, and it is not "Feisibuke" de, this time they are temporarily in a "probation" stage, to really declare an object death, at least to go through the process again mark.

  Provided that the target mark is performed strand reachability analysis found no reference to the GC Roots connected.

  1, marked the first time and the first screening:

    Does not cover the object finalize () method, the object will be directly recovered.

  2, the second mark:

    If the object covered by the finalize () method, as long as the re-associated with a reference to any object on the chain can be.

PS: How to determine the class of a class is useless

  Class needs to meet the following three conditions can be regarded as useless categories:

  1, all instances of the class have been recovered, that is, any instance of the class heap does not exist.

  2, the class loading ClassLoader has been recovered.

  3, corresponding to the class object does not have anywhere chanting java.lang.Class reference, not by the method of accessing the class reflected anywhere.

Guess you like

Origin www.cnblogs.com/jpxjx/p/12552790.html