Java garbage collection method to determine the object survived algorithm

Garbage collection:

        From time to time to clean up the heap unreachable objects. Unreachable object and not immediately will direct recycling, garbage collection in a Java program is automatic and can not be enforced, even if the programmer can definitely judge a piece of memory has been useless, should be recovered , the programmer can not force a garbage collector to reclaim the memory block. Programmers can do is by calling System.gc way to "recommend" the garbage collector, but whether it can be executed, when executed they are all unknown. This is the main disadvantage of the garbage collector. Of course, with respect to its enormous convenience to the programmer in terms of this disadvantage is outweighed the bad ones.

Unreachable objects: is not continue to refer, not continue to be used, not live objects are unreachable objects

User user = new User();

user = null; // user is not a reachable target

 

System.gc();

        Recommendations for the garbage collector to recover, but the execution or not, time is uncertain

 

finalize():

        Object object inside the method, finalize () method is to delete the object before the garbage collector calls this object. Subclasses override this method for organizing system resources, or do some other cleanup work.

A subclass overrides the finalize () method to organize system resources or perform other cleanup work.

 

The new generation, old years

 

 

1, java heap may be divided into a new generation, the old time, the default ratio 1: 2   The parameter values can be -XX: NewRatio specified

2, and can be divided into a new generation region Eden, From survivor (s0), To survivor (s1), the default, Edem: from: =. 8 to:. 1:. 1 (by parameter -XX: setting SurvivorRatio)

3, in most cases, the object is first allocated in eden region, after the new generation of recovery, if the object is still alive, then enter the area s0 or s1, after each time through the new generation of recovery, if the object is to survive its age on the increase after (15) 1, object reaches a certain age, then enter the old era.

 

Determination target survival:

    Reference counting has been eliminated :()

         If an object is not any reference points can be regarded as waste. The disadvantage of this method is that the presence of loop can not be detected.
         First need to declare at least the mainstream Java virtual machine inside did not choose to manage the memory reference counting algorithm. 
         What counts is the reference algorithm: adding a reference to the object counter, whenever a reference to its place, the counter value by 1; when referring to the failure, the counter is decreased by one at any time the counter value is no longer the object 0 It is used. Why mainstream Java virtual machine inside did not choose this algorithm do? The most important reason is that it is difficult to solve the problem of mutual circular references between objects.

   Root search algorithm
         basic idea root searching algorithm is through a series of objects called "GC Roots" as a starting point, to start the search downward from these nodes, called search path traversed reference chain (Reference Chain), when a when the object is not connected to any reference GC Roots chain, then it proves that this subject is not available.
         The basic idea of the algorithm is a series of objects called "GC Roots" as a starting point, a search downward from these nodes, called search path traversed chain of references, when an object has no references to GC Roots chain ( that GC Roots when the object is unreachable), then prove that this target is not available.
        In the Java language, as GCRoots object of the following categories:
       (1) Virtual Machine stack (local variables in a stack frame, also called the local variable table) referenced object.
       (2) The process region static property class object reference.
       (3) The object referenced method constant region.
      (4) in native method stacks JNI (Native Method) referenced object.

     In layman's terms, is to see the virtual machine stack of local variables, methods zone static properties, constants, as well as native method stacks there are no objects have references ...

1,2,4,6 can be drawn object instances have GC Roots accessibility, that is, live objects, objects that can not be recovered GC. 
For instance, although direct communication objects 3,5, but none GC Roots connected thereto, which is the GC Roots unreachable object, which is GC need to recycle garbage objects.

 

Guess you like

Origin www.cnblogs.com/pickKnow/p/11116797.html