Deep understanding of JVM Reading Notes-class loading mechanism

Source: Internet
Author: User

I. Overview

1. Definition: virtual machines extract the description class data fromClassThe file is loaded to the memory, and the data is verified, converted, parsed, and initialized to form a file that can be directly used by virtual machines.JavaType. The process of class loading and connection is completed during running.

Ii. Timing of class loading

1. lifecycle of class loading: loading ( Loading ) --> Verify ( Verification ) --> Prepare ( Preparation ) --> Resolution ( Resolution ) --> Initialize ( Initialization ) --> Use ( Using ) --> Uninstall ( Unloading )

2. Load: This is determined by the virtual machine.

3. Initialization phase:

A) encounteredNew,Getstatic,Putstatic,InvokestaticThis4If the class has not been initialized, initialize the class.

B) UseJava. Lang. ReflectClass reflection call.

C) When initializing a class, perform initialization if the parent class has not been initialized.

D) when starting a virtual machine, you must specify a primary class to be executed. The virtual machine first initializes the primary class.

Note: The initialization rules for interfaces and classes are different from those for Class 3. Do not initialize interfaces with all parent interfaces.

Iii. Process of class loading

1. Load

A) loading phase

I. Get the binary byte stream that defines this class using the full qualified name of a class.

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

III. InJavaGenerate a heap that represents this classJava. Lang. ClassObject.

B) after the loading phase is complete, the binary byte stream is stored in the square area according to the format required by the virtual machine.

2. Verification: The purpose of this phase is to ensureClassThe information contained in the byte stream of the file meets the requirements of the current virtual machine.

A) File Format verification: verify whether the byte stream conformsClassFile Format specifications, and can be processed by the current version of the virtual machine.

B) Metadata verification: Perform Semantic Analysis on the bytecode description to ensure that the description information conformsJavaLanguage specification requirements.

C) bytecode verification: the main task of this phase is to analyze the data stream and control flow. The task is to ensure that the methods of the verified class do not harm the security of virtual machines at runtime.

D) symbol reference verification: This phase occurs when the Virtual Machine converts the symbol reference to a direct reference (the parsing phase). It mainly verifies information other than the class itself. The purpose is to ensure that the parsing action can be executed normally.

3. preparation: the preparation phase is to formally allocate memory for the variables and set the initial values. These memories will be allocated in the method area. The variables here only include class scalar values and do not include instance variables.

4. Resolution: The parsing is the process in which the Virtual Machine replaces the symbol reference of the constant pool with the direct reference.

A) 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.

B) direct reference: direct reference can be a pointer pointing directly to the target, relative offset, or a handle that can indirectly locate the target. Direct drinking is related to the memory layout.

C) parsing of classes or interfaces

D) Field Parsing

E) Class Method Analysis

F) interface method Parsing

5. Initialization: Based onProgramInitialize variables and other resources in the subjective plan area developed by the operator, or express them in another way: the initialization stage is the execution class constructor.<Clinit> ()Method process.

 

Iv. class loaders

1. Classes and class loaders

A) For any class, the class loader that loads it needs to establish itsJVM.

2. parent-Child delegation model: the parent-child Delegation Model of the Class Loader requires that all classes except the top-level start-up class loaders should have their own parent class loaders, here, the parent-child relationship between class loaders is generally not inherited, but is used to combine the relationship to reuse the attachedCode.

3. working Process of the parent-child delegate model: If a Class Loader receives a class load request, it will not try to load the class by itself first, instead, the request is delegated to the parent class loader for completion. The class loaders at each layer are like this. Therefore, all the home requests should eventually be passed to the top-level startup class in the folder, only when the parent class reports that it cannot complete the load request will the parent class attempt to load it by itself.

4. A bit:JavaThe class loader has a hierarchical relationship with priority.

 

5. The class loader first acts as a proxy to other class loaders to try to load a class. This means that the class loader that truly completes the loading of the class and the class loader that starts the loading process may not be the same. To load a class, callDefineclassAnd the loading process of the startup class is through callingLoadclass. The former is called a class definition loader (Defining LoaderThe latter is called the initial loader (Initiating Loader).InJavaWhen the Virtual Machine determines whether the two classes are the same, it uses the class definition loader.. That is to say, it is not important to start the loading process of the class. What is important is to define the loader of the class. The association between the two types of loaders is that the definition loader of a class is the initial loader of other classes it references.

for A Class Loader instance, classes with the same full name are loaded only once, that is, loadclass methods are not repeatedly called.

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.