Java class loading process

Foreword:

A java file is loaded from the life process of being loaded to being unloaded
        -> linking (verification + preparation + parsing) -> initialization (preparation before use) -> use -> unloading
the whole process of loading the class:
        loading -> verification -> Preparation->Analysis->Initialization
details:
    1. The first is to load:

        This virtual machine has to do 3 things:

            1. Get the binary byte stream that defines this class by its fully qualified name.

            2. Convert the static storage structure represented by this byte stream into the runtime data structure of the method area.

            3. Generate a java.lang.Class object representing this class in the java heap as the access entry for these data in the method area.
                
        Explanation : Regarding the first point, it is very flexible. Many technologies are cut in here, because it does not limit where the binary stream comes from:
                    from class file -> general file loading
                    from zip package -> loading in jar The class
                    comes from the network -> Applet
    2, and after the loading is completed, it will start to check those byte streams

        The purpose of the inspection: to ensure that the byte stream information of the class file conforms to the taste of the JVM and does not make the JVM feel uncomfortable.

        The verification mainly goes through several steps: file format verification -> metadata verification -> bytecode verification -> symbol reference verification

            File format verification: Verify that the byte stream conforms to the specification of the Class file format and verify that its version can be processed by the current jvm version. After ok is no problem, the byte stream can be stored in the method area of ​​the memory. The next three checks are performed in the method area.

            Metadata verification: Semantic analysis is performed on the information described by the bytecode to ensure that the described content conforms to the syntax specification of the Java language.

            Bytecode verification: Verify that the bytecode file compiled by java is not damaged or formatted incorrectly

            Symbolic reference verification: to verify the authenticity and feasibility of some references. For example, if other classes are cited in the code, we need to check whether those classes exist or not. Or some properties of other classes are accessed in the code. The accessibility of those properties is checked. (This step will lay the foundation for subsequent parsing work)

    3. After the above steps are completed, it will enter the preparation stage:

        This stage allocates memory for class variables (referring to those static variables) and sets initial values, which are allocated in the method area. Static modified with final is not included here, because final will be allocated at compile time

    4. After completing the previous step, it is necessary to analyze. The parsing seems to be converting the fields, methods and other things of the class, which specifically involves the format and content of the Class file, and I don't have a deep understanding of it.

    5. In the previous class loading process, except that the user can participate in the loading stage through the custom class loader, other actions are completely dominated by the jvm, and the code in the java is actually executed after the initialization. This step will perform some pre-operations. Note that in the preparation phase, a system assignment has been performed for the class variable. In fact, to put it bluntly, this step is the constructor of the execution program

Here the computer only wrote the text version, and later updated an Xmind mind map,,,

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324986396&siteId=291194637