[JVM-5] class loading mechanism

Outline

Java has a dynamic class loading and dynamic connection characteristics, so the loading process is not like other languages has been done at compile time, it is the dynamics of that program is running dynamically loaded into memory.

Loading

Class load

Drawing, loading, verification, preparation, initialization, is determined unloading sequences of the five phases, the class loading process must start in this order step by step, but not necessarily resolve phase: in some cases it can be an initialization phase after the start, which is to support the operation of the Java language bindings when (also known as dynamic binding).

 

1, Load

Loading stage is completed outside the virtual machine stored in a binary byte stream format in accordance with the desired method in the virtual machine region. In order to complete this step which features need to complete it:

  1. Obtaining binary stream by the fully qualified name of a class;
  2. Static binary stream storage structure defined runtime data structures into the method area;
  3. Generating in memory a Class object representative of this class, as a method of access interface region data.

It should be noted that the above mentioned three steps are just part of the requirements of the specification, this requirement is actually relatively loose, a lot of things and there is no limit of very dead, for example, the first step of obtaining a binary stream, it does not requirements class binary stream must be retrieved from the file, and therefore, in use during class binary stream can be obtained from the network, and the like may be generated dynamically calculated.

2, verification

Verify role is to ensure that the byte stream file contains information consistent with current virtual machine requirements to ensure that it does not endanger the safety of the virtual machine. As said before Class files compiled from the source code is not all, people can manually modify the generated Class file, so this step verification work is very necessary. So what needs to verify all verify it:

File format validation

This step is mainly to ensure compliance with the information on the Java Class file format requirements. Such as file type, version number, constant pool, constant pool data and so on. . . . . .

In addition, in this step the byte stream will enter into the method area of ​​the memory, the latter method of operation are based on the structure of the storage area performed.

Metadata validation

Byte code description information semantic analysis, such as whether the class has a parent class, Overload correct, final, abstract there is no wrong, its main purpose is to metadata class semantic analysis, to ensure compliance with the Java language specification.

Bytecode verification

Control flow and data flow analysis. For example, the correct bytecode instruction set, the program jumps security. Its main purpose is to check vivo data security, to ensure that legal procedures semantic, logical.

Verification of symbolic references

Verification of symbolic references is also a special stage, which is analytic phase service (which also verified previously mentioned, these processes are not performed sequentially completed). In the parsing process, the symbolic reference virtual machine converts a direct reference, which is mainly made of check symbols matching reference constant pool. Inspection includes the following:

  1. Symbols can find references to the class;
  2. Specified class has no fields and methods described herein;
  3. Symbolic reference point to access all kinds of information is not right;
  4. 。。。。。。。。。。。。。。。

3, ready

Preparation phase is the official class variables and allocate memory for the set-up phase of its initial value, these variables are used by the memory allocation in the process area . On this point, there are two places to note:

1, this time for memory allocation only class variables (static variables modified) instead of instance variables, instance variables will be assigned together with the objects in the object instantiation heap in Java

2, this variable is assigned the initial value of the phase refers to those not modified final static variable, such as "public static int value = 123;", value is 0 instead of 123 after the preparation phase, to the value assigned to operation 123 We will only be in the initialization phase; such as "public static final int value = 123;" not the same, in the preparation stage, the virtual machine will be assigned to the value of 123.

Zero value of each type of data as shown below:

4, parsing

In the validation phase of said symbol references the validation phase is to resolve symbol references converted to a direct reference, then what symbolic references and direct references refer, respectively, then, what is the difference between them:

  • Symbol references. Is capable of any kind of literal unambiguous targets, regardless of its implementation of the virtual machine memory layout, the referenced target does not necessarily need to be loaded into memory;
  • Direct reference. Directly to the target pointer, reference offset, the virtual machine implementation, and associated memory layout, the target reference necessarily in memory.

In this step, the virtual machine classes / interfaces, field, class methods, interfaces, methods, parse, becomes a direct reference.

5, initialization

Initialization stage is to initialize class variables and other resources, mainly through the implementation of the class constructor <clint>()method.

<clint>()Operation is collected automatically assigned all classes and static variables of the statement block by a compiler ( static{}块) and in accordance with the order merge generation. Initialization phase to do is give static variable is assigned the value specified by the user and perform static code block .

When a virtual machine is not required to work other stages, but with different initialization phase. What happens when several virtual machines have to start initialization. (As before initiating loader, validation, preparation, step by step to resolve it all began).

  1. When faced with the following instruction in the bytecode level, new(the object to be generated, it must initialize a), get/put static(the use of static variables, it must assign a), invoke static(call the static method of all, you must assign a static amount );
  2. Reflection call. When using the java。lang。reflectmethod based on the reflecting call;
  3. Initialize a class, I found there initialize the parent class, you need to initialize the parent class, (the parent interfaces do not initialize immediately, only to its constant use, it needs to be initialized);
  4. Virtual machine requires one entry, the main class needs to be initialized;
  5. Dynamic analytic method, analytic method is a static method of another class, you need to initialize it.

Provision virtual machines and only more than 5 Ways need to initialize immediately, there are some calls, it looks like you need to initialize, but actually do not need, can be called passive call .

  1. Subclass direct use of static variables of the parent class. VM provides that only direct definition of the class need to initialize static variables, so in this case, it will only trigger initialization of the parent class, and subclass does not trigger;
  2. An array of objects. When defining an array of objects, it will only trigger initialize an array class, class objects within it is not initialized;
  3. When the constant call to another class of a class. At this time you will not have class constants (the caller) carried out initialized. The caller will only initialize. Because at compile time, according to the constant propagation optimization , constant changes the constant to be placed class constant pool caller, in which case these two classes has no connection, therefore there be initialized.

Guess you like

Origin www.cnblogs.com/Joy-Hu/p/11102548.html