Series | depth understanding of the Java virtual machine with three (class loading mechanism)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/baron_leizhang/article/details/99675625

Starting in the micro-channel public number: BaronTalk , welcome attention!

Previous article we introduced the "class file structure" , this one we look at how the virtual machine loads classes.

Then we can run the source code is compiled into byte code after compilation, eventually need to be loaded into the virtual machine. The virtual machine data that describes the classes loaded from Class file into memory, and verify the data, and converts parsing initialization, forming Java type can be used as a virtual machine, the virtual machine that is the class loading mechanism .

Different languages ​​and compile-time need to be connected to work, Java language class loading, connectivity and initialization is done during the program run, this strategy add some performance overhead While the class will be loaded, but will Java applications provide a high degree of flexibility, Java was born dynamically extensible language features is the dynamic loading and dynamic linking dependent on the operating characteristics during implementation.

For example, an interface-oriented application may wait until the specified actual implementation class runtime; users can Java predefined and custom class loader defined, so that a local application running loads from one network or elsewhere binary stream as part of the program code.

A. Classloading opportunity

Class from the virtual machine from loading to unloading, the entire life cycle include: Load (Loading), verification (Verification), preparation (Preparation), analytical (Resolution), initializing (Initialization), using (Using), unloading (Unloading) 7 stages. Wherein the verification, preparation, referred to as analytical connecting portion 3 (Linking). The order of occurrence of the seven stages below:

The figure above loading, validation, preparation, initialization sequence and unloaded five phases are determined, the class loading process must "pay attention to this order begin step by step, step by step here that is beginning, does not require the previous stage in order to complete the implementation of the next stage ", and not necessarily analytical stage: it can be re-started after the initialization phase, in some cases, this is to support the Java dynamic binding.

Virtual Machine Specification for when to start the loading process of the first node class "load" does not enforce constraints. But for the "initialization" phase, the virtual machine is the strict rules of one and five kinds of only the following circumstances, if the class is not initialized, it must be the class be "initialized" (load, validate, prepare natural need to start before that) immediately :

  1. Encounter new, getstatic, putstatic or invokestatic This 4-byte code instructions;
  2. Use java.lang.reflect package of reflection when the call classes;
  3. When initializing a class, parent class has not yet found the time to initialize, you need to trigger initialization of the parent class;
  4. When the virtual machine starts, the user needs to specify a main class to be executed, the virtual machine to initialize this class;
  5. When using a dynamic language support JDK 1.7, if a final example of the analysis result java.lang.invoke.MethodHandle REF_getStatic, REF_putStatic, REF_invokeStatic the method handle, and this handle corresponding to the class the method is not initialized.

"There is one and only" 5 or more scene will trigger initialization class, these five scenarios of behavior is called the initiative a reference to a class. In addition, all references to class methods are not trigger initialization, known as passive reference. For example, the following scenario is passive several references:

  1. Reference a static field of the parent class by subclass does not lead to initialize subclass;
  2. To refer to the class by the array definition, does not trigger this type of initialization;
  3. Constants at compile time will be credited to the constant pool class is called, there is no direct reference to the definition of the class constants of nature, it will not trigger initialization class defining constants;

II. Class loading process

load

Here's "load" refers to a phase of "class loading" process. In the loading phase, the virtual machine needs to complete the following three things:

  1. To obtain such a binary byte stream defined by the fully qualified name of a class;
  2. This represents the byte stream of static storage structure into a run-time data structure area method;
  3. Generating in memory represents java.lang.Class object of this class, as a method for accessing the various data entry areas of this class.

verification

The first step is to verify the connection phase, the purpose of this stage is to ensure that the information byte stream file included in Class meet the requirements of the current virtual machine and the virtual machine will not jeopardize their own safety. Validation phase will largely complete the inspection operation following four stages:

  1. File format validation : the first stage to verify whether the byte stream compliant Class file format, and can be the current version of virtual machine processing. Verification points include: whether the beginning of the magic number 0xCAFEBABE; major and minor version number in the current VM processing range; if there is the constant type is not supported Constants cell; various parts and documents Class file itself is there are deleted or added other information and so on.

  2. Metadata verify the parent of the class; if the parent class has:: The second stage is the byte code description information semantic analysis to ensure that the information which describes in line with the requirements of the Java language specification, this stage comprising verification point class is not allowed to be inherited inherited class; if this class is not an abstract class, whether to implement all the methods in the parent class or interface implementation requirements; class fields, methods, whether or not conflict with the parent class, and so on.

  3. Bytecode verification : The third stage is the most complex of the entire verification process of a stage, the main purpose is the data flow and control flow analysis to determine the semantics of the program is legitimate, logical.

  4. Verification of symbolic references : final stage parity generating symbolic references in the virtual machine into direct reference time, the conversion operation in the third phase of the connection - Analytical phase occurs. Verification of symbolic references can be seen as a class other than itself (constant pool reference symbols) for matching the image verification.

ready

Preparation phase is formally allocate memory for class variables and set the stage class variable initial values ​​of these variables used in memory will be allocated in the method area. This stage there are two concepts prone to confusion needs to be emphasized at:

  • First, this time including only memory allocation class variables (static variables to be modified), and does not include the instance variables, instance variables will be assigned when the object is instantiated along with the objects in the Java heap;

  • Second, the initial value here under "normal circumstances" is a zero-value data types. Suppose the definition of a class variable is public static int value = 123;then the variable valueinitial value 0 after the preparation stage 123 instead because the time has not yet execute any Java method, and the value assigned to putstatic instruction 123 after the program is compiled, stored in the class constructor among the devices () method, the value will be assigned to the operation 123 will be performed during the initialization phase.

Mentioned here, in "normal circumstances" the initial value is zero value, it will have some relatively "special circumstances": If there is ConstantsValue property field attribute table class field, in the preparation stage that will be initialized variable value ConstantValue attribute value referred to. Suppose the above class definition becomes a variable value public static final int value = 123;, JavaC ConstantValue property value is generated at compile time, during the preparation phase in accordance with the virtual machine will be provided ConstantValue value '123.

Resolve

Parsing stage is a virtual machine to a constant pool of symbolic references to the process of replacing direct references. Mentioned many times symbolic references and direct reference, then in the end what is symbolic references and direct reference to it?

  • Reference symbol (Symbolic Reference) : In a certain reference symbol set that describes the referenced symbols, the symbols can be any kind of literal, can be used as long as the unambiguous target to target.

  • A direct reference (Direct Reference) : direct reference may be direct pointer to the object, a relative offset or indirectly targeted to handle targets.

initialization

Category class initialization phase is the last step in the process of loading the foregoing class loading process, in addition to dominate and control the loading stage other than the user application defined by the class loader from participation, the remaining operation entirely by the virtual machine. To the initialization phase, really started classes defined in Java code. The initial stage is the implementation class constructor () method.

III. Classloader

Virtual machine design team in the class loading phase "to get the description of such a binary byte stream through a fully qualified name of the class" this action to the outside Java virtual machine to achieve, to allow the application to decide how to get the the necessary classes. Implement this action code modules called "class loader."

Class and class loader

For any category, you need to load it established by the class loader and the class itself with its uniqueness in the Java virtual machine, each class loader has a separate class namespace. In other words: To compare the two class is "equal", as long as these two classes is the premise makes sense to load the same class loader, otherwise, even if these two classes a Class from the same file, the same a virtual machine load, as long as the class loader to load them different, and that the two classes will surely not equal.

Parent delegation model

From the perspective of the Java virtual machine is concerned, there are only two different class loaders: one is to start class loader (Bootstrap ClassLoader), the class loader is implemented in C ++, it is part of the virtual machine itself; another that all other class loader, which consists of Java class loader to achieve, independent of external virtual machine, and all derived from the abstract class java.lang.ClassLoader.

From the perspective of Java developer's perspective, the class loader can be divided into:

  • Start class loader (Bootstrap ClassLoader) : This class loader is responsible will be stored in <JAVA_HOME> \ lib directory of libraries loaded into the virtual machine's memory. Start class loader can not be directly referenced Java program, users write custom class loader, if you need to load the delegates to start the class loader that can be used directly instead of null;

  • Extension class loader (ClassLoader the Extension) : This class loader by the sun.misc.Launcher$ExtClassLoaderimplementation that is responsible for loading <JAVA_HOME> \ All libraries, developers lib \ ext directory or the specified path system variables java.ext.dirs It can be used directly extended class loader;

  • Application class loader (the Application ClassLoader) : This class loader by the sun.misc.Launcher$App-ClassLoaderimplementation. getSystemClassLoader()This method returns the class loader is thus also be called a system class loader. It is responsible for loading the user class path (ClassPath) specified library. Developers can directly use this class loader, if the application is not too own custom class loader, in general, this program is the default class loader.

Our applications are mutually complementary loaded by these three types of loader, where necessary, can be in your own custom class loader. Their relationship is as shown below:

This hierarchy by showing the image above, the parents called the class loader delegation model (Parents Delegation Model). In addition to the parent delegation model requires top-level boot loader class, the rest of the class loader should have its own parent class loader.

Parent delegation model works like this: if a class loader loads the class received a request, it first does not own to try to load this class, but to delegate this request to the parent class loader to complete each level the class loader is true, so all requests ultimate load should be transferred to the top of the boot class loader, only when the parent class loader feedback they can not complete this class load request (in its search did not find when the class), sub-loader will try to load your own.

The advantage of this is that Java classes along with its class loader has a relationship with a priority level. For example java.lang.Object, rt.jar it on, regardless of which class loader to load the class are ultimately assigned to the boot class loader to load at the top of the model, so the procedure of the various classes Object class loader environments are the same class. On the contrary, if no parent delegation model, by each class loader to load their own words, if you have written a class called java.lang.Object and put ClassPath program, that there will be multi-system different Object class, Java type system, the basic behavior will not be guaranteed.

Delegation model is important for parents to ensure the stability of the Java program to run, but it's very simple to achieve, achieve parents delegate code model are concentrated in the java.lang.ClassLoader loadClass (method), the logic is clear: to check whether had been loaded, the loadClass () method is invoked if no parent class loader, when the loader is empty parent default boot loader as the parent class loader. If the parent fails to load, throw a ClassNotFoundException, then call their findClass () method to load.

protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException {
    // 首先,检查请求的类是不是已经被加载过
    Class<?> c = findLoadedClass(name);
    if (c == null) {
        try {
            if (parent != null) {
                c = parent.loadClass(name, false);
            } else {
                c = findBootstrapClassOrNull(name);
            }
        } catch (ClassNotFoundException e) {
            // 如果父类抛出 ClassNotFoundException 说明父类加载器无法完成加载
        }

        if (c == null) {
            // 如果父类加载器无法加载,则调用自己的 findClass 方法来进行类加载
            c = findClass(name);
        }
    }
    if (resolve) {
        resolveClass(c);
    }
    return c;
}

About the class structure and class files loaded on the adoption of a continuous two articles here, and the next we talk about "virtual machine bytecode execution engine."

References:

  • "In-depth understanding of the Java Virtual Machine: JVM advanced features and best practices (2nd edition)"

If you like my article, I will focus on the public under the number BaronTalk , know almost columns or GitHub add a Star on it!

Guess you like

Origin blog.csdn.net/baron_leizhang/article/details/99675625