In-depth understanding of Java Virtual Machine-----------virtual machine class loading mechanism

Source: Internet
Author: User

Virtual machine class loading mechanism

 Classes from being loaded into virtual machine memory, to unloading out of memory, the entire life cycle includes: load, verify, prepare, parse, initialize, use, uninstall, etc. 7 stages. wherein, the validation, preparation, parsing 3 parts is called a connection.

In the 7 phases above, loading, validating, preparing, initializing and unloading The five phases of the sequence are deterministic , and the loading process of the class must begin in such a sequential manner (beginning does not mean step-by-step "done" or "done", Because these phases are usually mixed with each other in a cross-cutting manner. The parsing phase is not necessarily : it can be started after the initialization phase in some cases, in order to support runtime bindings for the Java language.

The loading process of the class begins in the first phase is not determined, the virtual machine can be freely grasped according to the specific situation. for this phase of initialization , the virtual Machine specification strictly stipulates that only 5 cases must be immediately performed on the class:

1. When encountering new,getstatic,putstatic or invokestatic 4 byte codes, if not initialized, initialize immediately;

2. When using the Java.lang.reflect method to make a reflection call to a class, if the class has not been initialized, it needs to trigger its initialization first.

3. When initializing a class place, if it is found that its parent class has not yet been initialized, its parent class is initialized first;

4. When the virtual machine starts, the user needs to specify a host class to execute, the virtual opportunity to initialize the main class first;

5. When using JDK 1.7 language support, if a java.lang.invoke.MethodHandle instance finally resolves the result ref_getstatic, Ref_putstatic, ref_invokestatic method handle, And the class that corresponds to this method handle is not initialized, so it needs to be initialized immediately.

  There are only five cases in which the active trigger initialization (active reference)is initiated, otherwise it is not initialized ( passive reference ).

  The loading process of the interface is slightly different from the class loading process, there are some special instructions for the interface: The interface also has the initialization process, which is consistent with the class, the statements in the class are mostly using static statement block "static{}" to output initialization information, and interface species can not use "static{}" Statement block, but the compiler will still generate a "<clinit () >" class constructor for the interface that initializes the member variables defined by the interface species. interface and class initialization is different from the 3rd, the interface in the initialization of the process, does not require its parent interface all completed initialization, only when the actual use of the parent interface when the wood initialization.

  The process of class loading

1. "Load" is a phase of the "class loading" process, during the load phase, the virtual machine needs to complete the following 3 things:

1) obtain a binary stream that defines this class by using the fully qualified name of a class.

2) Convert the static storage structure represented by this byte stream into the runtime data structure of the method area.

3) Generate a Java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area.

In-depth understanding of Java Virtual Machine-----------virtual machine class loading mechanism

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.