当前位置:网站首页>Class loading process

Class loading process

2020-11-10 11:24:56 JAVA_ Mr. Park

The life cycle of a class

The complete life cycle of a class is as follows :

Class loading process

Class Files need to be loaded into the virtual machine before they can be run and used , So how does the virtual machine load these Class What about the documents ?

System loading Class There are three main steps in the type of document : load -> Connect -> initialization . The connection process can be divided into three steps : verification -> Get ready -> analysis .

load

The first step in the class loading process , Mainly complete the following 3 thing :

  1. Get the binary byte stream defining this class through the full class name
  2. Convert the static storage structure represented by the byte stream to the runtime data structure of the method area
  3. Generate an in memory... That represents the class Class object , As the access to the data in the method area

Virtual machine specifications are more than the above 3 The point is not specific , So it's very flexible . such as :“ Get the binary byte stream defining this class through the full class name ” It doesn't specify where to get 、 How to get . such as : It is more common to go from ZIP Read from the package ( In the future JAR、EAR、WAR The basis of format )、 Other file generation ( Typical applications are JSP) wait .

The loading phase of a non array class ( The action of getting the binary byte stream of a class during the loading phase ) It's the most controllable stage , In this step, we can finish it or customize the classloader to control the way to get the byte stream ( Override a classloader's  loadClass()  Method ). Array types are not created by class loaders , It consists of Java Virtual machine directly create .

Class loader 、 The parent delegation model is also a very important knowledge point , This part will be introduced separately in the following articles .

Part of the load phase and the connect phase are interleaved , The loading phase is not over , The connection phase may have started .

verification

 Schematic diagram of verification phase

Get ready

The preparation stage is the stage of formally allocating memory for class variables and setting the initial value of class variables , All of this memory will be allocated in the method area . There are several points to pay attention to in this stage :

  1. At this time, memory allocation only includes class variables (static), Not instance variables , Instance variables will be allocated along with a block of objects when they are instantiated Java In the pile .
  2. The initial value set here " General situation " The following is the default zero value of data type ( Such as 0、0L、null、false etc. ), For example, we defined public static int value=111 , that value The initial value of the variable in the preparation phase is 0 instead of 111( The initialization phase is assigned ). A special case : Such as to value Variable plus fianl keyword public static final int value=111 , So the preparation stage value The value of is assigned to 111.

Zero value of basic data type :

 Zero value of basic data type

analysis

In the parsing phase, the virtual machine replaces the symbolic reference in the constant pool with the direct reference . Parsing actions are mainly for classes or interfaces 、 Field 、 Class method 、 Interface method 、 Method type 、 Method handle and call qualifier 7 Class symbol reference .

Symbol reference is a set of symbols to describe the target , It can be any literal amount . Direct reference It's a direct pointer to the target 、 Relative offset or an indirect handle to the target . When the program is actually running , It's not enough to just quote symbols , for instance : When a program executes a method , The system needs to know exactly where this method is .Java The virtual machine has a method table for each class to store all the methods in the class . When you need to call a class's method , As long as you know the offset of this method in Fang publishing, you can call this method directly . By parsing the operation symbol reference, it can be directly transformed into the position of the target method in the method table of the class , So that the method can be called .

Sum up , In the parsing phase, the virtual machine replaces the symbolic reference in the constant pool with the direct reference , That is to get the class or field 、 Method pointer or offset in memory .

initialization

Initialization is the last step in class loading , It is also defined in the real execution class Java Program code ( Bytecode ), The initialization phase is to execute the initialization method  <clinit> () Method process .

about <clinit>()  Method call , The virtual machine itself ensures its security in a multithreaded environment . because  <clinit>()  The way to do this is to lock threads , So class initialization in multithreaded environment may cause deadlock , And this kind of deadlock is hard to find .

For the initialization phase , Virtual machines are strictly regulated and only 5 Under different circumstances , Class must be initialized ( Only active use of the class will initialize the class ):

  1. When you meet new 、 getstatic、putstatic or invokestatic this 4 When there are direct code instructions , such as new One class , Read a static field ( Not by final modification )、 Or when calling a static method of a class .

    • When jvm perform new Class is initialized when the . That is, when the program creates an instance object of a class .
    • When jvm perform getstatic Class is initialized when the . That is, the program accesses the static variables of the class ( Not a static constant , Constants are loaded into the runtime constant pool ).
    • When jvm perform putstatic Class is initialized when the . That is, the program assigns values to the static variables of the class .
    • When jvm perform invokestatic Class is initialized when the . That is, the program calls the static method of the class .
  2. Use  java.lang.reflect  When the method of package makes reflection call to the class, such as Class.forname("…"),newInstance() wait . , If the class is not initialized , It needs to trigger initialization .
  3. Initialize a class , If its parent class has not been initialized , Trigger the initialization of the parent class first .
  4. When the virtual machine starts , The user needs to define a main class to execute ( contain main Method type ), Virtual opportunity initializes this class first .
  5. MethodHandle and VarHandle It can be seen as a lightweight reflection call mechanism , And if you want to use this 2 Calls , You have to use it first findStaticVarHandle To initialize the class to be called .
  6. 「 Add , come from issue745  When an interface defines JDK8 New default method added ( By default Keyword decorated interface method ) when , If the implementation class of this interface is initialized , The interface must be initialized before it .

uninstall

Uninstall this part of the content from   issue#662 from   guang19  Complement and perfect .

Unload class is the class of Class The object is GC.

Unload classes need to satisfy 3 Requirements :

  1. All instance objects of this class have been GC, In other words, there is no instance object of this class in the heap .
  2. This class is not referenced anywhere else
  3. An instance of the class loader for this class has been GC

therefore , stay JVM Life cycle class , from jvm The class loaded by the built-in class loader will not be unloaded . However, the classes loaded by our custom class loader may be unloaded .

Just think about it ,jdk Self contained BootstrapClassLoader,PlatformClassLoader,AppClassLoader Responsible for loading jdk Class provided , So they ( An instance of a class loader ) Definitely not recycled . The instances of our custom class loader can be recycled , So the classes loaded with our custom loader can be unloaded .

Reference resources

版权声明
本文为[JAVA_ Mr. Park]所创,转载请带上原文链接,感谢