How to determine whether the object class is dead and useless class

When the JVM garbage collection is performed, the first thing to do is to find those objects that have no variable references, and thus the recovery of such objects.
So, JVM is how to determine the object has died of it?
 

First, reference counting
when a reference object is added to the program counter, each of which has a reference variable, a counter is incremented. When referring off counter is decremented. When the counter is 0, representing no variable references it, the object is the state of death, JVM requires the recovery of such objects.

Reference counting method to achieve a simple, high efficiency. But the vast majority of mainstream virtual machine did not take this memory management algorithm to count because this count algorithm can not recover those objects that have circular references to each other, such an object really is no longer in use, but because of the references to each other, leading to the respective counter is not zero, therefore they can not be recovered JVM.

 

 

Second, the reachability analysis
creates a series of GC Roots as a starting point, to start the search downward from these nodes, called search path traversed chain of references, when an object has no references to the GC Roots then linked chain, that this object to GC Roots unreachable, then prove that this target is not available, JVM such objects will be recovered later.

Most mainstream JVM such algorithms are used to manage memory, which can solve the problem of circulation between object references. Although the object between the object and the reference cycle, when they are no references to GC Roots chain, or the system determines that they are recyclable objects.

 When an object is determined by these two methods has no variable references them, JVM will be the recovery of such objects at the right time.

 

The main area of recovery method is useless class, how to tell if a class is useless like it?
Constant is determined whether or not a "waste constant" is relatively simple, and to determined whether a class is "useless class" is relatively many harsh conditions. Class needs to meet the following three conditions in order to be regarded as "useless class":

All instances of the class have been recovered, which is the Java heap any instance of the class does not exist.
Load class ClassLoader has been recovered.
Java.lang.Class corresponding to the class object is not referenced in any place, not by the method of accessing the class reflected anywhere.
Virtual machines can be kind of useless meet the above three conditions are recovered, said here is just "may", rather than as objects and not used will inevitably be recycled.

 

Guess you like

Origin www.cnblogs.com/luxianyu-s/p/11726211.html