I. Overview of virtual machine class loading
The virtual machine loads the class file that describes the classes into memory, verifies the data, transforms parsing and initialization, and eventually forms a Java type that can be used directly by the virtual machine
The Java language supports dynamic loading and dynamic connections.
Second, the specific process of virtual machine load class
1. General process of class loading
Load-to-connect (validate, prepare, parse)-to-initialize, it is important to note that there is no clear boundary between each stage, usually the previous stage is executed to a point where it triggers the next
Phase of implementation.
2. Specific tasks for each step in the class loading process
Loading Phase : 3 tasks need to be completed. The class file is obtained by the name of the classes (note: The source of the bytecode file can be arbitrary); Convert the static storage structure represented by this bytecode file
Is the run-time data structure for the method area; Generates an object in the Java heap that represents the Java.lang.Class file for the class bytecode file, which acts as a portal for the program to access the method area data.
Note: The data structure of the method area is defined by the virtual machine itself, and the loading phase is interleaved with the part of the connection phase.
validation : Validation is the first step in the connection, and the primary purpose is to ensure that the information in the bytecode file conforms to the requirements of the virtual machine and does not cause damage to the virtual machine. Different virtual machine-to-byte codes
Validation of the file may be different, mainly divided into 4 parts:
file Format Validation : Verifies the format specification of the bytecode file and verifies that it can be processed by the current version of the virtual machine. The bytecode file can only enter the method area if this part of the validation is completed.
The validation of the next 3 sections is based on method area validation.
meta-data validation : Semantic Analysis of the descriptive information of bytecode to ensure that descriptive information conforms to the Java language specification.
Bytecode Verification : the most complex part of the entire verification process, mainly the validation of data flow and control flow, ensuring that the class's methods are not harmful to the virtual machine at run time.
Symbolic Reference Validation : This phase occurs in the process of converting a virtual machine into a direct reference, which occurs during the parsing phase. The purpose of this phase is to ensure that the parsing phase
can be performed normally.
Preparation : The goal is to initialize all class variables-static member variables-by default.
Parse : Converts a symbolic reference in a virtual machine constant pool to a direct reference. Divided into 4 classes, class or interface parsing, field parsing, parsing of class methods, parsing of interface methods
Initialize : Executes the initialization of the bytecode file definition.
Iii. Class and Class loader
Class loader: In fact, a piece of code is used to implement the load of the class, but it is important to note that the class objects generated in the heap are not equal by loading with different classloader.
Class Loader classification: Starts the ClassLoader (part of the JVM), the Extension class loader, the application ClassLoader, and the custom ClassLoader. Usually the application is loaded by these 3 kinds of
Loader mates together to complete the loading of the class.
The class loader's parental delegation Model: If a class loader receives a class-loaded affinity, it does not come out first, but passes it to its superior classloader until the request is uploaded to the top-level startup ClassLoader.
The Start class loader decides whether to load the class in its own case, and the subclass loader attempts to load the class when it does not load the class. The greatest benefit of this approach guarantees the uniqueness of the class after it is loaded.
Java Virtual machine class loading mechanism--overview