JVM class loading mechanism comprehensive analysis

What is the class loading mechanism

The JVM loads class description data file into memory from Class, and verify the data, and converts parsing initialization, forming Java type can be directly used in the JVM, which is the JVM class loading mechanism.
If you are not familiar with the structure of Class file, you can refer to the article before the comprehensive analysis of Class file structure (on) and Class comprehensive analysis of the file structure (lower) .

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Class life cycle

From the class is loaded into memory, the memory is unloaded to the total divided into the following steps:

  1. Load (Loading)
  2. Verification (Verification)
  3. Preparation (Preparation)
  4. Parsing (Resolution)
  5. Initialization (Initialization)
  6. Use (Using)
  7. Uninstall (Unloading)

Class loading the whole process, which comprises the loading , verification , preparation , parsing , initialization phases.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

load

Load class is the first stage of loading, in this step JVM specification required to complete the following three things:

  1. To obtain a binary byte stream class definition by the fully qualified name of a class.
  2. Static storage structure represented by the byte stream into multiple run-time data structure area method.
  3. Generating a representative of this class java.lang.Class objects in memory.

In fact, the above requirements are not specific, JVM specific implementations and applications are more flexible. For example: Get this class binary byte stream, and where to obtain not say, how to obtain, so there is read (jar, war, ear) from the archive, the acquisition (the Applet) from the network, the compute runtime generating (dynamic proxy). For the load is not an array of class, we can define your own class loader to control the manner of obtaining the byte stream. However, for an array class is not the same as the class itself is not an array created by the class loader, but the JVM created directly.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

verification

This phase is to ensure that the information byte stream Class file contained in line with current requirements of the JVM, JVM and do not endanger its own security. Roughly divided into the following four stages:

File format validation

Verify the byte stream for compliance with Class file format, this can not be processed by the JVM. More verification points, such as: whether the beginning of the magic number 0xCAFEBABE, major and minor version numbers are within the scope of the current process JVM, constants constant pool of whether there is a constant unsupported type, CONSTANT_Utf8_info type of constant whether non-compliance with UTF8 the encoded data and the like. This stage is the verification flow based on binary bytes, only at this stage validated by the byte stream to enter the memory storage area method.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Metadata validation

This stage is mainly metadata information such semantic analysis and verification, to ensure that metadata information is not compliant with the Java language specification does not exist. For example: if there is in addition to the class other than the parent class java.lang.Object, whether inherited an inherited class is not permitted, whether the non-abstract class implements all the methods of its parent class or interface implementation requirements, whether covered parent the final field, and so on.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Bytecode verifier

By this stage the data flow and control flow analysis to ensure that the program is legal semantics, logical. For example: When placing the stack data types and operations used to ensure consistent, will not guarantee the jump instruction to jump to the bytecode instructions other than the method body, to ensure that body type conversion method is effective and the like.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Symbolic reference check

This phase is the information other than the type itself (various symbols constant pool reference) matching check, it occurs in the analytic step, to ensure analytical normal execution, such as: a reference symbol defined by a string of full-described if you can find the name of the corresponding class, whether the access method of class field of symbolic reference can access the current class, and so on.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

ready

At this stage, to allocate memory for static variables and set a static variable initial values. The initial value here that is usually the case, the initial value of the code is not written, but the zero value of the data type. Code written initial value in the initialization assignment phase. If it is a static constant (the final modification), this stage will be directly assigned to write code initial value.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Resolve

At this stage, JVM symbolic references to the constant pool is replaced with a direct reference. Symbol reference to a certain set of symbols described in the referenced, literal symbols may be in any form, can be unambiguously positioned to the target can be used as long as, it is irrelevant, and the memory layout JVM implementation. A direct reference may be a direct pointer to the target, a relative offset or indirectly targeted to handle the target, and it is the memory layout JVM implementation dependent. If you have a direct reference, the target reference must exist in memory.

Analytical primarily for the class or interface, fields, methods class, interface method, type method, and the method handle calls for symbolic reference points qualifiers, respectively corresponding to the constant pool CONSTANT_Class_info, CONSTANT_Fieldref_info, CONSTANT_Methodref_info, CONSTANT_InterfaceMethodref_info, CONSTANT_MethodType_info, CONSTANT_MethodHandle_info and CONSTANT_InvokeDynamic_info.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

initialization

Really started the initialization phase defined in the class bytecode, but also perform the class constructor () Process approach. () Method is implemented by the compiler automatically collected class all the static variables assignment operation and static statements statement in the block merger sequentially compiler collection was washed sequentially with statement appears in the source file as determined static statements block access only to static variables defined in a block of statements before, the definition of variables behind it, static blocks can be assigned, but can not be accessed.

JVM will ensure that the subclass () Before performing the method, the parent class () Method has been executed, block static statements that is defined in the parent class priority subclass in the variable assignment. If the class is not a static block of statements, there is no assignment of static variables, the compiler will not be generated for this class ()method. Interface () Method does not need to run a parent interface () Method, only when the variable defined in the interface using the parent, the parent will be the interface is initialized.

JVM will ensure a class () Method is correctly locked in a multi-threaded environment, synchronization. If a thread in the implementation of the class () Method, other threads are blocked need to wait, when () Method after the implementation of the other threads will not enter again ()method. Under the same class loader, a class is initialized only once.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Epilogue

This time we know the class loading process of several stages, namely, loading , validation , preparation , analytical and initialization . Loading is the binary byte code into memory, authentication information included in the check byte stream meets the requirements when, to prepare a memory allocated as a static variable and a static variable initial value setting, parsing is to replace symbolic reference constant pool is a direct quote, initialization is executed all motion and static variables static assignment statement statement in the block.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Guess you like

Origin www.cnblogs.com/heihaozi/p/11980670.html