The article role

  • Understand the life cycle of a class from load, connect, initialize to unload
  • Understanding class loading, class loaders; Understand the parental delegation model
  • Understand and master the initialization practices of various active use classes

Class loading life cycle (Overview)

The entire life cycle of a class begins when it is loaded into the JVM and ends when it is unloaded out of memory

  • Load: Finds and loads the binary data of the class file
  • Connect: Combines the binary data of classes that have been read into memory into the JVM runtime environment
    • Validation: To ensure the correctness of the classes being loaded
    • Preparation: Allocates memory for static variables of the class and initializes them
    • Parse: Convert symbolic references in the constant pool to direct references
  • Initialization: Assigns initial values to static variables of a class

Class loading

  1. Gets the binary byte stream of a class by its fully qualified name
  2. Converts binary byte flows into a runtime data structure for the method area
  3. Create a java.lang.Class object on the heap that encapsulates the data structure of the Class in the method area and provides an interface to access the data structure in the method area

The way classes are loaded

  • The most common way: load from a local file system, or from an archive such as a JAR
  • Dynamic: Dynamically compile Java source files into class files
  • Other methods: Download from the Internet and load from a proprietary database

Class loader

  • Java programs cannot directly reference the start classLoader. Set the classLoader to null, and the start classLoader is used by default
  • Class loaders do not need to wait for “first active use” of a class before loading it; the Jvm specification allows class loaders to anticipate that a class will be used
  • If the.class file is missing during loading, LinkageError will be reported when the class is actively used for the first time. If it has not been used, no error will be reported

Loader type

  • Java VIRTUAL machine built-in loader
    • Start the class loader BootstrapClassLoader
      • Description: used to load startup based module class, such as Java. The base, Java. Management, Java. The XML, and so on
    • Platform classloader (JDK8 or above) PlatformClassLoader
      • Used to load some platform-related modules
    • Extended class loaders (JDK8 and below)ExtensionClassLoader
      • Note: PlatformClassLoader is changed to PlatformClassLoader after 8, because ExtensionClassLoader will load loaders defined in ext folder, which is not safe
    • Start the class loader AppClassLoader
      • Description: Used to load application-level modules
  • User-defined loader: it is a subclass of java.lang.ClassLoader. You can customize the loading mode of the class. It’s just that custom class loaders are loaded last of all system class loaders

Class loader relationship

Parental delegation model

  • The parental delegation model is important to ensure the stable operation of Java programs
  • The code that implements parental delegation is in the loadClass() method of java.lang.classloader, and it is recommended to override the findClass() method if you are customizing the ClassLoader

Classloaders in the Jvm typically use the parent delegate model, requiring that all classloaders have their own parent loader in addition to starting the ClassLoader. The parent-child relationship here is composed rather than inherited, and the working process is shown below

Break the parent delegate model

  • One problem with the parent model is that the parent loader cannot downward identify resources loaded by the child loader
  • To solve this problem, a Thread Thread Thread class loader is introduced, which can be set via Thread setContextClassLoader()
  • Another typical situation is when hot replacement is implemented, such as OSGI’s modular hot deployment, where class loaders no longer strictly follow the parental delegation model and many may be executed in flat class loaders

Type of connection

validation

  • Class file structure check: Follow the class file structure specified by the JVM specification
  • Metadata validation: Semantic analysis of the information described by bytecode to ensure compliance with Java language specifications
  • Bytecode validation: Ensuring that program semantics are legal and logical by analyzing data flow and control flow. The method body is mainly verified here
  • Symbolic reference validation: Checks for matches between symbolic references in the constant pool and information outside the class itself

To prepare

Allocate memory for static variables of the class and initialize them

parsing

Parsing is the process of converting symbolic references in the constant pool into direct references, including: symbolic references: a set of unambiguous symbols describing the referenced object, independent of the implementation of the virtual machine

Direct reference: refers directly to the target pointer, relative offset, or handles that can be indirectly located to the target

Mainly for: class, interface, field, class method, interface method, method type, method handle, call point qualifier

Class initialization

Class initialization is the process of assigning initial values to static variables of a class, or executing class constructor methods

  1. If the class is not already loaded and connected, load and connect first
  2. If a class has a parent class and the parent class is not initialized, the parent class is initialized first
  3. If there are initialization statements (such as static blocks) in the class, they are executed in turn
  4. If it’s an interface
    • When a class is initialized, the interface it implements is not initialized first
    • When an interface is initialized, its parent interface is not initialized
    • Interface initialization occurs only when a program first uses a variable in an interface or calls an interface method
  5. Calling the loadClass method of the Classloader class to load a class does not initialize the class and is not an active use of the class

Initialization time

Java programs use classes in active and passive ways. The JVM must initialize each class or interface when it is first actively used. Passive use of a class does not result in class initialization. Active use is as follows:

  1. Creating a class instance
  2. A static variable that accesses a class or interface
  3. Call a static method of a class
  4. The reflection classes
  5. Initializes a subclass of a class whose parent class has not been initialized
  6. The main class that runs when the JVM starts
  7. Defines the interface of the default method when the interface implements class initialization

Initialization will not occur if:

  1. Subclasses refer to static fields of the parent class and do not initialize the child class
  2. Referring to a class through an array definition does not trigger initialization
  3. Accessing static constants

The use of the class

It’s literally used. Skip it

Class of unloading

Understand that the JVM automatically determines and completes the class unload

When the Class object representing a Class is no longer referenced, the life of the Class object ends and the corresponding data in the method area is unloaded

Classes loaded by the JVM’s own classloader are not unloaded. Classes loaded by a user-defined classloader can be unloaded