Drill-down loading mechanisms like Java virtual machines

Source: Internet
Author: User

Objective:

We have learned the class file format and the contents of the content, and have also learned the contents of the parts of the data area at run time. The next step is to learn how the JVM loads the information recorded in the class file into the runtime memory, and where the information for each part of the class file is stored in the runtime data area. What can we get from this text?

1.虚拟机是如何加载Class文件的2.Class文件信息进入JVM后有那些变化3.进一步理解运行时数据区、Class文件信息、以及类加载过程中都做了那些操作、java语言特性的根基

Class loading mechanism

The virtual machine loads the data of the description class from the class file into memory, verifies and initializes the data, and finally forms the Java type that can be used directly by the virtual machine, which is the class loading mechanism.

See here I have a few questions, do not know whether you have doubts, as follows:

1.虚拟机什么时候加载Class文件到内存的?2.虚拟机具体是用哪些部件加载Class文件到内存的?3.在利用某些部件加载Class文件时又具体做了那些操作?

Class load Time
A class from loading into memory, to unloading from memory for the entire life cycle, has a total of up to experience:
Seven phases of loading, validating, preparing, parsing, initializing, using, and uninstalling.
Note:
validation, preparation, parsing, and unification are called link stages.

加载、验证、准备、初始化、卸载这五个阶段顺序是确定的,而解析阶段则不一定。它在**某些情况**下可以在初始化之后开始,这就是java为什么支持**动态绑定**。

So we know the class load classification of these stages, then the class loading phase one: When did the load stage (Loading) start? There is no mandatory requirement in the virtual machine specification, which can be grasped according to the virtual machine specific implementation. However, there are four conditions in the initialization phase that are strictly defined in the virtual machine specification, which must begin the initialization phase immediately.

1.遇到new、getstatic、putstatic、invokestatic这四条字节码指令时。如果没有初始化,那么就必须触发初始化。2.使用java.lang.reflect包方法对类进行反射调用时,如果没有初始化,必须触发初始化。3.当初始化一个类时,其父类没有被初始化,那么需先触发父类初始化。4.当虚拟机启动时,用户需要指定一个要执行的主类(main类,主入口),虚拟机会先初始化这个主类。备注:有且只有这四种情况,虚拟机会对一个类**主动引用**。除此之外的所有类的引用,都不会触发初始化,称为**被动引用**。

See here, I still confused, do not know the first stage of the class loading process: The loading phase is the beginning of God code. Let's think about how we normally run a Java application or program. is not all starting from a main class run, because the development tools to help us block a lot of the underlying details, so I now remember that when we click on the main portal run, is not the start of the virtual machine, and specify the main class to execute the main class, that is, the above mentioned in the fourth case. But the virtual machine starts and executes the details of the main class I do not know at the bottom of the details of the operation, I first dug a hole here, I fill in the back.

Here I know that the entire class loading process should start with the main class loading of the fourth case.

Load phase
During the load phase, the virtual machine needs to do three things:

1.通过一个类的全限定名来获取定义此类的二进制字节流2.将这个字节流所代表的静态存储结构转化为方法区的运行时数据结构3.在java堆中生成一个代表这个类的java.lang.Class对象,作为方法区这些数据的访问入口

The three points in the virtual machine specification are not specific, very broad, such as the acquisition of binary byte stream, and does not say where it is obtained from the class file, or can be obtained from the network, which gives developers a great deal of flexibility.

Get from zip file: Eventually becomes the basis for the jar,war,ear format.
Get from the network: for example, an applet.
Run-time COMPUTE generation: The most used is dynamic agent technology, Java.lang.reflect.Proxy, is to use the Proxygenerator.generateproxyclass to generate for a specific interface. The binary byte stream of the $proxy proxy class.
Generated by other files: such as JSP
and other ways to get it. It is also very flexible for who to load this binary byte stream, which can be loaded using the system-provided ClassLoader or by a custom ClassLoader. See here we also know is the class loader to load the byte stream into memory .

The

validation phase
Virtual machine specification does not explicitly specify which aspects to check, how to check, when to check, different virtual machine implementations may be different, but most will complete the following four validations:
file format validation
requires to verify that the byte stream conforms to the specification of the class file format . Whether it can be executed by the current version of the virtual machine. The validation of this item is based on the class file format, for example, is the magic number 0xCAFEBABE? The minor version number, the major version number, is the current virtual machine can handle the scope of this version? Whether there are constant types that are not supported in the constant pool (tag check) and so on. This validation is to ensure that the input byte stream is parsed correctly and stored in the method area . Only the byte stream validated by the file format can be stored in the method area, and this validation is dependent on the byte stream. Subsequent validations are based on the validation of the method area storage structure.
Metadata Validation
This phase of validation is a semantic analysis of the information described in bytecode, verifying compliance with the Java language Specification . Whether there is a parent class, whether to inherit a non-inheritable class, if it is not an abstract class, whether to implement the parent class or interface methods, class fields, methods, and the parent class is inconsistent with the checksum, and so on.

BYTE code Verification
This phase verification is the most complex, it mainly carries on the data flow and the control flow analysis, guarantees the class method does not make the behavior which harms the virtual machine security in the operation. Also note that it is not safe to verify by bytecode, but by not being able to represent a certain security. For example, a dead loop. The high complexity of data flow validation, in order to avoid the time spent in the bytecode check phase, the Code property of the method body is added stackmaptable properties, the optimization is in the jdk1.6 after the Javac compiler, through the-xx:- The Usesplitverifier option is enabled for optimization, or the start-xx:failovertooldverifier requires a failure to revert back to the old version of type deduction validation. After jdk1.7, a class file with a major version number greater than 50 can only have a data flow analysis check.
Symbol Reference Validation
This phase occurs when a virtual machine triggers a symbolic reference to a direct reference, which occurs during the parsing phase. Usually checks: whether the class can be found, whether there is a description of the field that conforms to the method, the method that the simple name describes, the field, the class, the method, whether the access adornment of the field can be accessed by the current class, and so on. This phase is validated to ensure that the parsing action is performed properly. If the stage does not pass, then the subclass of the Java.lang.IncompatibleClassChangeError exception is thrown, such as: illegal access exception, no such field exception, no such method exception.

Note:
The validation phase is not necessarily the stage to be executed, and if used during development testing and repeated validation, you can reduce the time of class loading by setting:-xverify:none, shutting down most of the class validation measures.

Preparation phase

This phase is a phase that formally allocates the initial values of memory settings for class variables, all in the method area. The initial value here is not the given literal in the program, but the initial value given by the system (also called a 0 value).

Parsing phase
This stage is the stage in which the symbolic references in the constant pool are converted to direct references. Mainly for classes or interfaces, fields, class methods, interface methods, four types of symbolic references to parse.

Initialization phase
This stage should be said to be the developer very much, because this is written according to Java bytecode set the value of the variable, or reference. From another point of view is the implementation of the construction method stage, this stage some more detailed behavior, such as the class or interface of their parent or parent interface of the construction method, static statements, such as the execution of the order is not written in detail here, interested in self-access to information, Or look into the initialization phase of the seventh chapter of the Java Virtual machine.

So write to here the class loading process has been finished, and finally summed up:
1. The class loading process includes five stages of loading, validating, preparing, parsing, and initializing.
2. Class loading is loaded by the class loader of the virtual machine or the developer's custom ClassLoader
3. Each stage of the class load has some operations that revolve around the class file structure, the Java language Specification, and the virtual machine security.

Do you have a more comprehensive understanding of the heap loading process here? In addition to the virtual machine class loader, custom class loader in-depth understanding I will also after reading the book after the summary. If I make a mistake or understand the error above, please leave a message.

Drill-down loading mechanisms like Java virtual machines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.