[Grocery store] JVM # Java Virtual Machine loading mechanism

Code compilation of results from the local machine code into a byte code, is a small step in the development of storage format, but it is a big step in the development of the programming language - "in-depth understanding of the Java Virtual Machine"

The virtual machine data that describes the classes loaded from Class file into memory, and data verification, parsing and conversion initialization , forming the Java type can be used as a virtual machine, which is a virtual machine class loading mechanism.

Type loading, connectivity and initialization are in a program run during completion, although the increased overhead of running time, but it greatly increases the flexibility of Java, dynamic loading and easy connection. Java not only can be obtained from Class file belongs to, you can also obtain a network such as a binary stream data directly from other places, which greatly improves the ductility of Java.

opportunity

Class life cycle

Class from start loading to the unloading process after a total of seven, as shown below.

Wherein the verification, preparation, resolved collectively connected. In addition, the loading, validation, preparation, initialization, and uninstall this process is only beginning to 5 in the order can be executed at the same time, without waiting until after the end of the implementation of a process. For example, I began to prepare the 9:00, 9:10 start initialization, 9:20 ready to end.

Initialization timing

And only the following five cases, it can only be called "initialization":

  1. Encounter new, getstatic, putstatic, invokestatic four bytecode instructions and found class is not initialized, it is initialized. Of which about new understanding, in addition to generating an ordinary class instance, when a static method call will trigger the class initialization.
  2. The use of classes of packets java.lang.reflect reflection when calling.
  3. When a class to initialize the parent class has not yet found the time to initialize, you first need to initialize the parent class.
  4. When (class main () method where) the main class virtual machine boot time initialization to be executed.
  5. When using JDK1.7 + version of the dynamic language support, we found that class does not require initialization initialization.

In addition, all references to class methods are not trigger initialization, only to be called a passive reference.

Open a small difference, a static class code block, if a variable is assigned in advance, can be used; if only after a variable assignment, the error will be used in a static block. But whenever the assignment, as long as declared in the static code block, then the assignment is allowed. Look at this example:

public class Test{
    static{
        i=0;//给变量赋值可以正常编译通过
        System.out.print(i);//这句编译器会提示"非法向前引用"
    }
    static int i=1;
}

For the interface, the one and only the first three cases will be referred to as initialization. In addition, the interface does not need to meet in advance so that parent interface initialization time unless you helpful to the parent interface.

process

Gradually look loading, validation, preparation, analytical and initialize these five processes.

load

Loading process need to do three things:

  1. To obtain such a binary byte stream by the full name of the class defined.
  2. The byte stream that represents static storage structure into a run-time data structure area method.
  3. Representative types of memory and then generates a java.lang.Class object of this class, different from other common objects such an object, in the method area.

For non-array type, the load can be provided by a virtual class loader may customize the load by a user. For an array type, array itself is not loaded by the loader, but by the Java virtual machine created directly, the array elements are created by the loader.

After loading, the memory will give java.lang.Class object class, for subsequent bedding.

verification

While loading begins, verify choose open. Verification is to ensure that the information byte stream file containing species Class meets the last chapter talking about specifications, will not endanger the virtual machine itself. At this stage whether rigorous, will determine whether the Java virtual machine can withstand malicious code, from the perspective of execution performance, workload validation phase of the virtual machine class loader subsystem has accounted for a large part of it.

File format validation

First need to verify compliance with specifications Class file formats, such as magic number (coffee baby) exists and that major and minor version numbers may be running the current virtual machine, constant type of tag signs and so on. After verifying this stage based on a binary byte stream, and only through this validation stage, the byte stream will enter the method area in memory for storage, three verify later stages are all based on the method area storage structure no longer operate directly byte stream.

Metadata validation

This process includes verifying whether a parent, the parent class is allowed to be inherited ah, ah, and so whether various modifiers conflict.

Bytecode verification

The data flow and control flow analysis determines when the main object semantics of the program is legitimate, logical. This process ensures that the operand stack at any time and the data type of the instruction code sequence can cooperate to ensure that the jump to the jump instruction is not the instruction byte code other than the method body, to ensure that the type of conversion is normal, to ensure parent between the field and subclasses do not conflict, and so on.

Since the data flow is very complex validation, to slow down the time consumed since the beginning of JDK1.6, Code attribute table attribute added to the method body as a "StackMapTable" attribute, the attribute describes a method for substantially all of the body Piece. During the bytecode verification, there is no need to push the program according to the legitimacy of these states, only StackMapTable inspection records attribute to legality. It saves time bytecode verification.

Verification of symbolic references

This stage occurs in the virtual machine is converted into symbolic references direct reference time, the conversion operation will be resolved in the third phase of the connection time occurs. Can be found by the need to verify the fully qualified class name of the string, whether the specified class field descriptor fields and methods and the method described in the simple name, classes, methods, fields, etc. of access.

ready

Preparation phase is formally allocate memory for class variables and set the stage class variable initial values. At this time, the static variable is set to an initial value of zero value, particularly the value of the code is not set, the specific required values ​​will set the initial code when putstatic instruction is executed. This static variable is final unless modified would have a set value code directly at this time.

Resolve

Parsing stage is a virtual machine to a constant pool of symbolic references replaced by a direct reference to the process.

Reference symbol: a symbol reference target set of symbols to be described in the referenced, literal symbols may be in any form, can be positioned unambiguously as long as the goal to use. Symbol reference memory has nothing to do with the distribution of virtual machines to achieve the target reference does not necessarily have been loaded into memory.

Direct references: a direct reference may be direct pointer to the object, a relative offset or indirectly targeted to handle targets. And is a direct reference to the memory layout of the virtual machine implementation of relevant references translated in a different virtual machine instances are generally not the same as a direct reference to a symbol. If you have a direct reference to that target reference must already exist in memory.

In addition to invokedynamic instruction, virtual machine implementations may cache the results of the first resolution. invokedynamic instruction is a dynamic language support related to instruction, can not do caching.

initialization

The final step in the process of class initialization time class load. The previous operation in addition to the custom class loader, is the leading virtual machine operating, initialization phase, began full implementation of Java code defined in the class.

Phase initialization process executed class constructor <client> () method. <client> () method is implemented by the compiler automatically collected class assignment operation all class variables and static statements statement in the block merger sequentially compiler collection it was in order of appearance by the statement in the source file of the decision . Constructor parent class <client> () method does not need to display their configuration has been good, and the static block of code is the first parent class to the static code block subclass. And <client> () method executes when the interlocking of different threads executing this method may appear thread blocking phenomenon.

Class loader

The virtual machine design team class loading phase "to get such a binary byte stream through a fully qualified class name of" this action to the outside to realize the Java virtual machine to achieve this action is called code block class loader .

Class and class loader

For any class, we need to determine its uniqueness in the Java virtual machine by loading its class loader and the class itself together. If a class is equal, then the two classes must be loaded in the same class loader. Class used herein may be equal equals method, IsAssignableFrom () method, isInstance () method validation, instanceof keyword may be used to make a judgment object affiliations. For example, fully qualified names are com.pjjlt.MyTest. A virtual machine loaded with its own class loader, with a user-defined class loader, then the two classes is not equal, object instances are generated only by the scope of the instanceof keyword will own class It is true.

Parents delegate mechanism

So the question is, I use self-defined class loader to load an Object into memory, it would mean that the whole basis of full-featured Java waste. In actual fact, the new Object class and also the Object class native is treated the same. This involves the parents delegate mechanism.

The angle of the virtual machine, only the virtual machine class loader and a user-defined type is described. For users, there is a startup class loader (Bootstrap ClassLoader), to expand the class loader (Extension ClassLoader), application class loader (Application ClassLoader) just a few, and they are to a combined complex relationship with the parent loader.

Parents delegate mechanism works: If a class loader loads the class received a request, it first does not own to try to load this class, but to the delegates to the parent loader to complete each level of class loaders are so, therefore all load requests should be sent to the final top-level boot loader class, only it can not load their own time feedback, will give the child loader loads.

This also explains why you wrote the Object class loader created and a native of the same, because people will not be loaded by writing your own class loader, loader but a parent layer loaded.

Guess you like

Origin www.cnblogs.com/pjjlt/p/11615748.html