Determining whether an object can be recovered

A reference counting algorithm:

  Determining the number of references to objects:

    Determining by reference to the number of objects to determine whether the object can be recovered;

    Each object instance has a reference counter, the referenced +1 or -1 reference completed;

    Any instance of a reference count of the object 0 may be regarded as garbage collection;

  Advantages and disadvantages:

    Advantages: high efficiency, less affected by the implementation of the program;

    Disadvantages: can not detect that the circular references that cause memory leaks;

Second, the reachability analysis algorithm:

Determining whether the object referenced by the chain up to determine whether the object can be recovered;

When do jvm garbage collection, we must first determine whether an object is also likely to be used. So how to determine whether an object is also likely to be used?

If our program can no longer refer to the object, then the object can certainly be recovered, this condition is called unreachable. When the object is not reachable, the object can be recovered by a garbage collected.

Then the up or not up to how to judge it?

The answer is GC roots, is the root object, if there is no path to reach the root object from an object, or from the root object can not be a reference to the object that is unreachable.

The following three types of objects in jvm as GC roots, to determine whether an object can be recycled 
(Normally we only know virtual machine stack and static reference is enough)

  • Objects in the virtual stack machine (JVM stack) referenced (accurate to say that the virtual machine stack stack frame (frames)) 
    We know every method of execution, jvm will create a corresponding stack frame (stack frame included the operand stack, local variable table, runtime constant pool references), the stack frame that contains all the objects used within the method of reference (of course, there are other types of data base), when the implementation of the latter method, the stack frame it will pop up a virtual machine from the stack, so that references temporary objects created would not exist, or that there is no gc roots pointing to these temporary objects, which will be recycled out on the next GC

  • Method zone class object reference to the static properties 
    of static attribute is the type (class) of the property, does not belong to any single instance, and therefore the property as naturally gc roots. As long as there is this class, the references to objects will always exist. class can also be recovered, described later surface

  • Native method stacks (Native Stack) referenced objects

A class to be accurate to say that the recovery should be unloaded, must meet the following three conditions

    • Heap any instance of the class does not exist
    • The classloader load the class has been recovered
    • Class java.lang.Class object is not referenced anywhere, with access to information that is no longer the class through reflection

Guess you like

Origin www.cnblogs.com/jxxblogs/p/12208113.html