What is the life cycle of the .class file in java (that is, the process of class loading to class release)

table of Contents

1. The life cycle of a class

2. Active reference and passive reference

3. When is the class (.class) released?


1. The life cycle of a class

The bytecode file (.calss file) needs to go through the following process from being executed by JVM to release:

The complete life cycle of a java class will go through five stages of loading, connecting, initializing, using, and unloading. Of course, there are also cases where it is used directly without being initialized after loading or connecting, as shown in the figure:

 

After the java class is loaded and when the use phase is completed, the java class enters the unloading phase, which is the so-called release zhi.


2. Active reference and passive reference

It is generally divided into two situations:

For active and passive quoting , refer to the article.

  • Active reference: the class will be initialized;
  • Passive reference: the class will not be initialized.

 


3. When is the class (.class) released?

Regarding the unloading of the class, after the class is used, the class will be unloaded if the following conditions are met:

  • There are no instances . All instances of this class have been recycled, that is, no instances of this class exist in the java heap.
  • The corresponding class loader is recycled . The ClassLoader that loaded this class has been recycled.
  • The .Class of this class is no longer referenced . The java.lang.Class object corresponding to this class is not referenced anywhere, and the methods of this class cannot be accessed anywhere through reflection.

If all the above three conditions are met , JVM will unload the class during garbage collection in the method area. The unloading process of the class is actually to clear the class information in the method area, and the entire life cycle of the java class is over.

Guess you like

Origin blog.csdn.net/Longtermevolution/article/details/107855823