Java Advanced Chapter (third, JVM compilation mechanism, class loading mechanism)

Source: Internet
Author: User

First, the loading process of the class

The JVM divides the load of classes into 3 steps:

1. Load (load)

2. Links (link)

3. Initialization (Initialize)

The link also has 3 steps, as shown in:

1) Load: Find and load binary data for classes (Find and import class files)

Loading is the first stage of the class loading process, and during the load phase, the virtual machine needs to complete the following three things:

1. Get the binary byte stream defined by the fully qualified name of a 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.

In contrast to other stages of class loading, the load phase (accurately, the action of getting the binary byte stream of a class during the load phase) is the strongest stage, because developers can either use the system-provided classloader to complete the load or customize their own classloader to complete the load.

When the load phase is complete, the binary byte stream outside the virtual machine is stored in the method area in the format required by the virtual machine, and an object of the Java.lang.Class class is created in the Java heap so that the data in the method area can be accessed through the object.


2) links (3 steps)


1. Validation: Ensure that the class being loaded is correct

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 validation phase will roughly complete the 4-phase inspection action:

File Format verification: Verify that the byte stream conforms to the specification of the class file format, for example: whether to start with 0xCAFEBABE, whether the major and minor version numbers are within the processing range of the current virtual machine, and whether the constants in the constant pool have unsupported types.

Meta-data validation: Semantic analysis of the information described in bytecode (note: Comparing the semantic analysis of the JAVAC compilation phase) to ensure that the information it describes conforms to the requirements of the Java language specification, for example: Whether this class has a parent class, except Java.lang.Object.

Bytecode verification: Through data flow and control flow analysis, it is reasonable to determine that the program semantics are legal and logical.

Symbol Reference Validation: Ensures that parsing actions are performed correctly.

The validation phase is important, but not necessary, and it has no effect on the program runtime, and if the referenced classes are repeatedly validated, consider using the-xverifynone parameter to turn off most of the class validation measures to shorten the load time of the virtual machine class.


2. Prepare: Allocate memory for static variables of the class and initialize them to default values

The prep phase is a phase that formally allocates memory for class variables and sets the initial value of class variables, which are allocated in the method area. There are a few things to note about this phase:

1. This time memory allocation includes only class variables (static), not instance variables, and instance variables are allocated to the Java heap as objects are instantiated when the object is instanced.

2, the initial value set here is usually the default value of the data type 0 (such as 0, 0L, NULL, FALSE, etc.), rather than being explicitly assigned in the Java code value.

Suppose a class variable is defined as: public static int value = 3, then the initial value of the variable value after the prep phase is 0, not 3, because no Java method has started executing at this time, The putstatic instruction, which assigns value to 3, is stored in the class constructor <clinit> () method after the program is compiled, so the action of assigning value to 3 will not be executed until the initialization stage.

3. Parsing: Converting a symbolic reference in a class to a direct reference

The parsing phase is the process by which a virtual machine replaces a symbolic reference within a constant pool with a direct reference, and the parsing action is primarily for a class or interface, a field, a class method, an interface method, a method type, a method handle, and a call qualifier Class 7 symbol reference. A symbolic reference is a set of symbols that describe a target, which can be any literal.
A direct reference is a pointer directly to the target, a relative offset, or a handle that is indirectly anchored to the target.


3) Initialize: Static variable for class, initialization operation

initialization, which assigns the correct initial value to the static variables of the class, the JVM is responsible for initializing the class, primarily initializing the class variables. There are two ways to set the initial value of a class variable in Java:

① declares that the class variable is the specified initial value.

② uses a static code block to specify the initial value for the class variable.



Initialization of the class

When the class is initialized:

1) Create an instance of the class, that is, the new object

2) Access a static variable of a class or interface, or assign a value to the static variable

3) Calling the static method of the class

4) Reflection (Class.forName ("Com.lyj.load"))

5) Initializes a subclass of a class (the parent class of the child class is initialized first)

6) The startup class indicated at the start of the JVM, that is, a class with the same file name and class name, only those 6 cases will cause the class's class to be initialized.


Initialization steps for classes/JVM initialization steps:

1) If this class has not been loaded and linked, then load and link it first

2) If the class has a direct parent class, and the class has not been initialized (note: In a classloader, the class can only be initialized once), then initialize the immediate parent class (not for the interface)

3) If there are initialization statements (such as static variables and static blocks) in the class, then these initialization statements are executed sequentially.


Loading of classes

Class loading refers to reading the binary data in the class's. class file into memory, placing it in the method area of the run-time data area, and then creating a Java.lang.Class object of this class in the heap that encapsulates the class's objects in the method area class.

The end product of a class that is loaded is a class object that is located in the heap area. The class object encapsulates the data structure of the classes within the method area, and provides the Java programmer with an interface to access the data structures within the method area.

There are several ways to load classes:

1) load directly from the local system

2) Download the. class file over the network

3) load. class files from archive files such as Zip,jar

4) Extract the. class file from the proprietary database

5) Dynamically compile Java source files into a. class file (server)

6) The command line starts when the application is loaded by the JVM initialization

7) Dynamic loading via Class.forName () method

8) Dynamic loading via Classloader.loadclass () method


Loading device

The class loading of the JVM is done through ClassLoader and its subclasses, and the hierarchy and loading order of the classes can be described as follows:

1) Bootstrap ClassLoader is responsible for loading the jar packages specified in all the class or Xbootclassoath options in the $java_home Jre/lib/rt.jar. Implemented by C + +, not the ClassLoader subclass.

2) Extension ClassLoader is responsible for loading some jar packages that extend functionality in the JAVA platform, including the jar packages in $java_home Jre/lib/*.jar or-djava.ext.dirs specified directories.

3) APP ClassLoader is responsible for loading the jar packages specified in Classpath and the classes and jar packages under the directory specified in Djava.class.path.

4) Custom ClassLoader the class by Java.lang.ClassLoader subclass, which belongs to the ClassLoader that the application customizes according to its own needs, such as Tomcat, JBoss will implement its own ClassLoader according to the Java EE specification.

The load process checks to see if the class is loaded, the check order is bottom-up, the custom ClassLoader to bootstrap ClassLoader-by-layer check, as long as a certain classloader has been loaded, Ensure 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.


End Life cycle

In the following scenarios, the Java Virtual machine will end the life cycle

1, the implementation of the System.exit () method

2, the normal execution of the program ends

3, the program in the execution process encountered an exception or error and abnormal termination

4. The Java Virtual machine process terminates due to an operating system error

Java Advanced Chapter (third, JVM compilation mechanism, 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.