Java class loading details, java loading details
1. class loading process;
The JVM divides the class loading process into three steps: load, link, and initialize. The link is divided into three steps;
Verification (varification), Preparation (Preparation), and Resolution (Resolution );
In fact, the entire lifecycle of a class starts from being loaded into the VM memory until the memory is detached:Load, verify, prepare, parse, initialize, use, and uninstallThese seven phases. Where,Verification, preparation, and resolution are collectively referred to as connections).
The order of loading, verification, preparation, initialization, and uninstallation is fixed, the class loading process must follow this sequence of steps to "start" (only refers to the start, rather than the execution or end, because these stages are usually mixed with each other, it usually calls or activates another stage in the execution of one stage, but the parsing stage is not necessarily (it can start after the initialization stage in some cases, this is to support runtime binding in Java.
A: loading: The loading phase is the first phase of class loading. In this phase, the virtual machine must complete three tasks.
1. Get the binary byte stream defining the class by using the full qualified name of the class, that is, to find and load the binary data of the 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 that represents the Class in the java heap and serve as the access entry to the data in the method area.
In the loading phase, you can use the Class Loader provided by the system to complete the process, or you can use a custom class loader to complete the process.
Part of the content in the loading and connection phases (for example, part of the bytecode file format Verification Action) is performed in a crossover manner. The loading phase has not been completed yet, and the connection phase may have started.
JVM in memory:
B Verification:
Verification is the first step in the connection phase. The purpose of this phase is to ensure that the information contained in the byte stream of the Class file meets the requirements of the current virtual machine and does not endanger the security of the virtual machine itself.
The Java language itself is a relatively secure language. Using Java encoding means that you cannot access data outside the array boundary or convert an object to an unimplemented type. If so,
The compiler rejects compilation. However, the Class file is not necessarily compiled by the Java source code, and can be used in any way,
This includes writing in a hexadecimal Editor (such as UltraEdit. If harmful "code" (byte stream) is directly written, and the virtual machine does not check when loading the Class, the security of virtual machines or programs may be compromised.
Different virtual machines may have different implementation methods for class verification, but the following four phases of verification are generally completed:
C preparation:
The standby stage allocates memory for the static variables of the class and initializes it to the default value. These memories will be allocated in the method area.
The preparation phase does not allocate the memory of instance variables in the class. instance variables will be allocated to the Java heap along with the object during Object Instantiation.
Public static int value = 123; // The initial value of value in the preparation phase is 0. It will only change to 123 in the initialization phase.
D parsing:
In the parsing phase, the virtual machine replaces the symbol reference in the constant pool with the direct reference.
Symbolic Reference: a Symbolic Reference describes a referenced object with a set of symbols. A Symbolic Reference can be a literal in any form and can be used to locate the object without ambiguity. Symbol reference has nothing to do with the memory layout implemented by the virtual machine. The referenced target is not necessarily loaded into the memory.
Direct Reference: Direct Reference can be a pointer directly pointing to the target, a relative offset, or a handle that can be indirectly located to the target. Direct reference is related to the memory layout implemented by the Virtual Machine. If there is direct reference, the referenced target must already exist in the memory.
E initialization:
Class initialization is the last step in the class loading process. In addition to the previous class loading process, the user application can participate in the class loading process through the custom class loader, other actions are completely dominated and controlled by virtual machines. The Java program code defined in the execution class is started only in the initialization phase.
The initialization phase is the process of executing the <clinit> () method of the class constructor. The <clinit> () method is generated by the compiler automatically collecting the values of all class variables in the class and merging the statements in the static statement block (static.
2. About class initialization: When can initialization be triggered?
That is, the first phase in which the class loading process needs to be started: "loading ". There is no forcible constraint in the virtual machine specification. This can be left to the virtual machine's specific implementation for free,
However, the specification for virtual machines in the initialization phase strictly specifies the following situations. classes will be initialized if they are not initialized.
The above is called a class
"Active reference"In this case, class initialization is not triggered.
"Passive reference"The interface loading process is slightly different from the class loading process. The static {} block cannot be used in the interface. When an interface is initialized, it does not require its parent interface to be fully initialized. It is initialized only when the parent interface is actually used (for example, a constant defined in the reference interface. 3. The class loader JVM performs class loading through ClassLoader and its subclass. The class hierarchy and loading sequence can be described as follows:
1) Bootstrap ClassLoader
Loads all the classes in jre/lib/rt. jar in $ JAVA_HOME, implemented by C ++, not the ClassLoader subclass.
2) Extension ClassLoader
Loads some jar packages for the extended functions of the java platform, including the jar packages under the specified directory of jre/lib/*. jar or-Djava. ext. dirs in $ JAVA_HOME.
3) App ClassLoader
Records the specified jar package in classpath and the class in the directory.
4) Custom ClassLoader
ClassLoader defined by applications according to their own needs. For example, tomcat and jboss implement ClassLoader based on j2ee specifications.
During the loading process, the system first checks whether the class has been loaded. The check sequence is from bottom to top, from Custom ClassLoader to BootStrap ClassLoader,
As long as a classloader has been loaded, this class is considered to have been loaded, so that only one ClassLoader is loaded. The order of loading is from top to bottom, that is, the upper layer tries to load this type.
From: http://www.cnblogs.com/javaee6/p/3714716.html http://m.blog.csdn.net/gjanyanlig/article/details/6818655