Java class loading details

Source: Internet
Author: User

1, the loading process of the class;

The JVM divides the class loading process into three steps: Load (load), link, and Initialize (initialize), where the link is divided into three steps;

Validation (varification), preparation (preparation), reconciliation (Resolution);

In fact, the class starts from being loaded into the virtual machine memory until it is unloaded, and its entire lifecycle includes the 7 phases of loading, validating, preparing, parsing, initializing, using, and uninstalling . wherein, the validation, preparation and resolution of these three parts are collectively referred to as the connection (linking).

Where the order of the five stages of loading, validating, preparing, initializing, and unloading is determined, the loading process of the class must be "started" in this order (only the beginning, not the execution or the end, as these phases are usually cross-mixed, It is common to invoke or activate another phase during one phase of execution, while 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.)

A: Load: The load phase is the first stage of class loading, in which virtual machines complete three things

1. Obtain a binary byte stream that defines this class by fully qualified name of a class, that is, to find and load the binary data of the class.

2. Transform 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 the Java heap as the access entry for the data in the method area.

The load phase can be done using the system-provided classloader, or it can be done by a user-defined ClassLoader.

The load phase and part of the connection phase, such as a partial bytecode file format validation action, are interleaved, the loading phase is not complete, and the connection phase may have started.

The way the JVM behaves in memory:

B Authentication:

Validation is the first step in the connection phase, which is designed to ensure that the information contained in the byte stream of a class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself.

The Java language itself is a relatively safe language, and using Java encoding is not possible, such as accessing data outside the bounds of an array, transforming an object into a type that it does not implement, and so on,

The compiler will reject the compilation. However, the class file is not necessarily compiled by Java source code, you can use any way,

This includes writing directly with a hex editor (such as UltraEdit). If a malicious "code" (byte stream) is written directly, and the virtual machine does not check when it loads the class, it can compromise the security of the virtual machine or program.

Depending on the virtual machine, the implementation of the class validation may be different, but the following four phases of validation will be done roughly:

C Preparation:

The standby phase allocates memory for the static variables of the class and initializes them to the default values, which are allocated in the method area.

The prepare phase does not allocate memory for instance variables in the class, and instance variables are allocated in the Java heap along with the objects as they are instantiated.

public static int value=123;//in the prep phase value has an initial values of 0. It will not change to 123 during the initialization phase.

D parsing:

The parsing phase is the process by which a virtual machine replaces a symbolic reference within a constant pool with a direct reference.

Symbol reference (Symbolic Reference): a symbol reference is a set of symbols to describe the referenced target, the symbol can be any form of the literal, as long as the use can be used without ambiguity to locate the target. The symbolic reference is independent of the memory layout implemented by the virtual machine, and the referenced target is not necessarily loaded into memory.

Direct Reference: A direct reference can be a pointer to a target directly, a relative offset, or a handle that can be indirectly anchored to the target. The direct reference is related to the memory layout implemented by the virtual machine, and if there is a direct reference, the target of the reference must already exist in memory.

E initialization:

Class initialization is the last step in the class loading process, and the rest of the action is completely dominated and controlled by the virtual machine, except in the load phase, where the user application can participate through the custom ClassLoader. In the initialization phase, the Java program code defined in the class is actually started.

The initialization phase is the process of executing the class constructor <clinit> () method. The <clinit> () method is generated by the compiler's automatic collection of assignment actions for all class variables in the class and statements in the static statement block (static{} block).

2, about initialization of class: When can I trigger initialization?

That is, the first phase of the class loading process that needs to begin: "Load". Virtual machine specification does not impose constraints, this can be given to the virtual machine of the specific implementation of free grasp,

However, for the initialization phase of the virtual machine specification, the following conditions are strictly specified, if the class is not initialized, the class is initialized.

    1. To create an instance of a class
    2. To access static variables of a class (except for constants "static variables that are final rhetoric" cause: Constants a special variable, because the compiler treats them as values (value) rather than field (field). If your code uses a constant variable (constant variable), the compiler does not generate bytecode to load the value of the field from the object, but instead directly inserts the value into the byte code. This is a useful optimization, but if you need to change the value of the final field, every piece of code that uses that field needs to be recompiled.
    3. Accessing static methods of a class
    4. Reflection as (class.forname ("My.xyz.Test"))
    5. When initializing a class, it is found that its parent class has not yet been initialized, then the initialization of the parent class is started
    6. When the virtual machine starts, the class that defines the main () method is initialized first.
The above conditions are called a class"Active Reference", except in this case, the initialization of the class is not triggered, called"passive Reference"The loading process of an interface is slightly different from the loading process of a class. The static{} block cannot be used in an interface.  When an interface is initialized, it does not require all of its parent interfaces to be initialized, only if it is actually used on the parent interface (for example, constants defined in the reference interface). 3, class loading of the class loader JVM is done through ClassLoader and its subclasses, and the hierarchy and loading order of classes can be described as follows:

1) Bootstrap ClassLoader

Responsible for loading all classes in Jre/lib/rt.jar in $java_home, implemented by C + +, not classloader subclasses

2) Extension ClassLoader

Some jar packages that are responsible for loading the extensions in the JAVA platform, including the jar packages in $java_home Jre/lib/*.jar or-djava.ext.dirs specified directories

3) App ClassLoader

Responsible for documenting the jar packages specified in the Classpath and the class in the directory

4) Custom ClassLoader

The ClassLoader that the application customizes according to its own needs, such as Tomcat and JBoss, will be implemented according to the Java EE specification itself ClassLoader

The load process checks to see if the class is loaded and the order is bottom-up, from custom ClassLoader to bootstrap ClassLoader-by-layer inspection,

This class is considered loaded as long as a classloader has been loaded, ensuring that only all classloader of this class are loaded once. The order of loading is top-down, that is, the upper layer tries to load the class one at a level.

This article originates from: http://www.cnblogs.com/javaee6/p/3714716.html http://m.blog.csdn.net/gjanyanlig/article/details/6818655

Java class loading details

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.