How does Java tell if an object is dead?

Almost all object instances in the Java world are stored in the Java heap. Before the garbage collector collects the heap, the first thing to do is to determine which of these objects are still alive and which are dead.

1: Reference counting method: The main reason is that this algorithm cannot solve the problem of mutual circular references between objects. The Java language does not use reference counting, so I won't introduce it here.

2: Root search algorithm (GC Roots), Java actually uses the GC Roots algorithm. In the Java language, objects that can be used as GC Roots include the following:

  • The referenced object in the virtual machine stack (local variable table in the stack frame)
  • Objects referenced by static properties in the method area
  • The object referenced by the constant in the method area
  • The object referenced by JNI (generally speaking Native method) in the native method stack

So how to judge a "useless class"? , you need to meet the following three conditions at the same time to be regarded as "useless class"

  • All instances of this class have been reclaimed, that is, there are no instances of this class in the Java heap.
  • The ClassLoader that loaded the class has been recycled
  • The java.lang.Class object corresponding to this class is not referenced anywhere, and the methods of this class cannot be accessed by radiation anywhere.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325056713&siteId=291194637