JVM-class loading mechanism and jvm-Loading Mechanism

Source: Internet
Author: User

JVM-class loading mechanism and jvm-Loading Mechanism
Virtual Machine Loading Mechanism

The Virtual Machine loads the class data described from the class file to the memory, verifies the data, converts the parsing and initialization, and finally forms the Java type that can be directly used by the virtual machine, this is the class loading mechanism of virtual machines.

Class loading time

Class is loaded to the VM memory, until the memory is detached. Its entire lifecycle includes: Loading, Verification, Preparation, Resolution, Initialization, and Using) and Unloading. The verification, preparation, and resolution are collectively referred to as Linking ).

The virtual machine specification strictly specifies that classes must be "initialized" immediately in only five cases ":

Class loading process

I. Loading

(1) In the loading phase, the virtual machine must complete the following three tasks:

(2) The array class itself is not created through the Class Loader. It is directly created by the Java Virtual Machine.

The creation of an array class follows the following rules:

 

Ii. Verification

Verification is the first step in the connection phase. The purpose of this phase is to ensure that the byte stream of the class file conforms to the requirements of the current virtual machine and does not endanger the security of the local virtual machine.

In the verification phase, the following four phases of verification are completed: File Format verification, metadata verification, bytecode verification, and symbol reference verification.

(1) File Format Verification

(2) Metadata Verification

(3) bytecode Verification

(4) symbol reference verification

  

Iii. Preparation

The preparation phase is the phase in which the memory is officially allocated to the class variables and the initial values of the class variables are set. The memory used by these variables will be allocated in the method area.

 

Iv. Analysis

(1) In the parsing phase, the virtual machine replaces the symbol reference in the constant pool with the direct reference.

(2) the virtual machine specification does not specify the specific time when the parsing phase occurs. Only anewarray, checkcast, getfield, getstatic, instanceof, invokedynamic, invokeinterface, invokespecial, invokestatic, invokevirtual, before the 16 bytecode commands for operating symbol reference, ldc, ldc_w, multianewarray, new, putField, and putstatic are parsed.

(3) The parsing action mainly applies to class or interface, field, class method, interface method, method type, method handle, and call point qualifier Class 7 symbol reference.

(4) parsing of classes or interfaces

The entire parsing process of the virtual machine requires the following three steps:

1) if c is not an array type, the VM will pass the fully qualified name representing N to the Class Loader D to load the class C. During the loading process, metadata verification and bytecode verification may trigger loading of other related classes.

2) if C is an array type and the element type of the array is an object, the array type will be loaded according to the above rules. If the N descriptor is in the preceding format, the type of the element needs to be loaded. Then, the virtual machine generates an array object representing the dimension and element of the array.

3) If no exception occurs in the above steps, C has actually become a valid class or interface in the virtual machine, but symbol reference verification must be performed after the parsing is complete, check whether D has access to C.

(5) Field Parsing

(6) Class Method Analysis

(7) interface method Parsing

 

V. Initialization

  

Class Loader

I. Class and Class Loaders

For any class, the loader that loads it and the class itself need to establish its uniqueness in the Java Virtual Machine. Each class loader has an independent class namespace.

Ii. Parental Assignment Model

(1) From the Java Virtual Machine perspective, there are only two different Class loaders: one is to start the Class Loader (Bootstrap Class ClassLoader ), this class loader is automatically implemented by the Java Virtual Machine Using the C ++ language. The other is all other class loaders which are implemented by the Java language, it is independent from the outside of the Virtual Machine and inherits from the abstract class java. lang. classLoader.

(2) subdivision: Start the Class Loader (Bootstrap ClassLoader)

Extension ClassLoadert)

Application ClassLoader [system class loader]

(3) Working Process of the parent-child assignment model:

If a Class Loader receives a request from the class loader, it first delegates the request to the parent class loader instead of trying to load the class by itself, this is true for class loaders at each layer. Therefore, all loading requests should eventually be sent to the top-level class loaders. Only when the parent class loader reports that it cannot complete the class loading request, the subclass loader will try to load it on its own.

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.