JVM and other loading processes

Source: Internet
Author: User
# Class Lifecycle

1. Load Loading
2. Verify Verification
3. Prepare preparation
4. parse resoluation
5. initialize Initialization
6. Use Using
7. Uninstall unloading

# Loading

Step 1 get the binary byte stream of the class using the fully qualified name of the class

Main Methods:
Obtained Through zip, jar, ear, and war formats
Get through network
Dynamic generation at runtime, dynamic proxy technology. In the proxy class, the proxygenerator. generateproxyclass method is used to generate the binary byte stream of the proxy class for a specific interface.
Class generated by JSP files
Obtain from database

Note: developers implement custom class loaders to control the way byte streams are obtained (that is, override the loadclass method)

The loading method of array classes is different from that of general classes:
The array classes are created by different class loaders, which are directly created by JVM.
The array class is closely related to the Class Loader. The element type of the array class is also created by the class loader.

Step 2 stores binary byte streams of classes in the method area, and the data structure is implemented by JVM.

Step 3 instantiate a java. Lang. Class Object in the memory
The class object is not necessarily in the Java heap. For the hotspot virtual machine, it is stored in the method zone.

The loading and connection phases (Verification preparation and resolution) are cross-organized.

 

# Verification

Because bytecode files are not necessarily compiled by Java source code, they can be obtained through various channels, which may lead to harmful bytecode, if the JVM loads harmful bytecode, the JVM will crash. Therefore, the purpose of verification is to ensure that the information contained in the bytecode file's byte stream meets the requirements of the current JVM, it does not harm the security of virtual machines.

 

Four phases:

1. Verify File Format: verify whether the byte stream complies with the bytecode file format specification

2. Verify metadata: whether it complies with Java language specifications

3. Verify bytecode: analyze the semantics and logic of the program through the data flow control flow

4. Verify that the symbolic reference takes place in the parsing phase

 

# Preparation

Allocate memory for class variables (modified by static) and set the class variable initial value. The memory will be allocated to the method area.

Example:

Public static int values = 123;

The initial value of the class variable in the preparation stage is 0, and the action of assigning the value to 123 occurs in the initialization stage.

 

When a constant is compiled, javac will generate the constantvalue attribute for the value and assign the value to 123 in the preparation phase.

 

Instance variables will be allocated to the Java heap together with the object during Object Instantiation.

 

# Parsing

JVM replaces symbol reference in the constant pool with direct reference.

 

Four parsing processes:

1. parsing of classes or interfaces

2. Field Parsing

3. Class Method Parsing

4. Interface Method Parsing

 

# Initialization

Java program code defined in the execution class. The static statement block is executed in this step.

 

# Trigger time of class initialization

Initialization is triggered when the class is actively referenced.
1. instantiate an object using new
2. When operating static fields (except constants) or static methods
3. reflection when calling a class
4. When initializing a subclass, if its parent class is not initialized, the initialization of the parent class is triggered first.
5. When JVM is started, the main class (class containing the main method) will be initialized first)
6. jdk7 dynamic language...

 

Initialization is not triggered for passive class references.

1. reference the static fields of the parent class by subclass. Only the parent class is initialized instead of the subclass.
2. Defining referenced classes through arrays does not trigger class initialization.
'''
Superclass [] SC = new superclass [10];
'''
3. Reading constants does not trigger initialization of classes that define constants.
Note: constants are stored in the constant pool of the call class during compilation. Essentially, they are not directly referenced to classes that define constants.

JVM and other loading processes

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.