Class loader in-depth analysis and stage decomposition (2)

1. Class loading

  • In the java code, the type loading, connection and initialization process are all completed during the running of the program.
  • Provides greater flexibility and adds more possibilities.

2. Life cycle of Java virtual machine and program

In the following situations, the virtual machine will end its life cycle:

  • Implemented System.exist ()
  • The program ends normally
  • Abnormal termination occurred during program execution
  • Java virtual machine process terminated due to an operating system error

3. The life cycle of the object

(1) Load: Find and load the binary data of the class

Description:
Read the binary data of the class .class file into the memory, place it in the method area of ​​the runtime data, and then create a java.lang.Classdata structure in the memory that encapsulates the class in the method area (java specification and It is not specified where the Class object is located. The HotSpot virtual machine places it in the method area).

Ways to load .class files:

  • Load directly from the local system
  • Download .class files via the Internet
  • Load class files from archive files such as zip and jar
  • Lift the class file from the proprietary database
  • Dynamically compile java source files into class files

(2) Connection stage:

  • Verification: ensure the correctness of the loading class
  • Preparation: Allocate memory for class static variables and initialize them to default values
  • Resolution: Convert symbolic references in the class to direct references

(3) Initialization: assign the correct initial value to the static variables of the class

  • All Java virtual machine implementations must be actively used by Java programs for the first time before each class or interface is initialized

(4) Use: use stage in the program code

  • Active use
  • Passive use

(5) Unloading: The Class object will end its life cycle when it is not being referenced, and the data of the class in the method area will also be unloaded

Class declaration cycle

Instructions for active and passive use:

  • Active use (7 types):

    (1) Create an instance of a class (putstatic)
    (2) Access a static variable of a class or interface, or assign a value to the static variable (getstatic)
    (3) Call a static method of the class (invokestatic)
    (4) Reflection, such as: Class.forName("com.test.Test")
    (5) sub-class initialization of a class, is actively used to identify the parent class of the class
    (6) when the Java virtual machine started to be marked to start class class (the class of the Main method)
    (7) JDK1.7 began offering Support for dynamic languages: the class corresponding to the handle java.lang.invoke.MethodHandleof the parsed result of an instance is GEF_GetStatic,REF_GETStatic,REF_InvokeStaticinitialized if it is not initialized

    • Passive use:

    In addition to the above 7 cases, other ways of using java classes are considered as passive use of the class, and will not cause the initialization of the class

    Insert picture description here

Published 41 original articles · Liked 14 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/Yunwei_Zheng/article/details/104326805