current position:Home>The JVM's class loading mechanism is fully analyzed, and the nginx practical core knowledge points are downloaded

The JVM's class loading mechanism is fully analyzed, and the nginx practical core knowledge points are downloaded

2021-08-26 11:12:30 Programmer Ning

JVM Describe the class data from Class File loaded into memory , And verify the data 、 Transform resolution and initialization , The final form can be JVM Direct use Java type , This is it. JVM Class loading mechanism of .

The life cycle of a class

Class from being loaded into memory , To be unloaded out of memory , It is divided into the following steps :

  1. load (Loading)

  2. verification (Verification)

  3. Get ready (Preparation)

  4. analysis (Resolution)

  5. initialization (Initialization)

  6. Use (Using)

  7. uninstall (Unloading)

The whole process of class loading , Including load verification Get ready analysis initialization Several stages .

load

Loading is the first stage of class loading , In this step JVM The following three things have been completed in accordance with the specification :

  1. Get the binary byte stream that defines a class by its fully qualified name .

  2. The static storage structure represented by the byte stream is transformed into the runtime data structure of the method area .

  3. Generate a representation of this class in memory java.lang.Class object .

The above requirements are not specific ,JVM The specific implementation and application of are relatively flexible . such as : Get the binary byte stream of this class , I didn't say where to get , How to get , So there's reading from the compressed package (jar、war、ear)、 Get it from the network (Applet)、 Runtime compute generation ( A dynamic proxy ). For class loading that is not an array , We can define our own classloader to control how the byte stream is obtained . however , It's different for array classes , Because array classes themselves are not created by class loaders , It is JVM Directly created .

verification

This stage is to ensure Class The information contained in the byte stream of the file matches the current JVM The requirements of , And it doesn't harm JVM Own safety . It can be roughly divided into the following four stages :

File format validation

Verify that the byte stream matches Class Specification of document format , Can it be JVM Handle . There are more verification points , such as : Whether to count by magic 0xCAFEBABE start 、 Whether the primary and secondary version number is current JVM Within the scope of 、 Whether constant of constant pool has unsupported constant type 、CONSTANT_Utf8_info Is there any inconsistency in the constant of type UTF8 Encoded data, etc . This stage is based on the binary byte stream for verification , Only this stage of validation passed , Byte stream can enter the method area of memory to store .

Metadata validation

This stage is mainly for semantic analysis and verification of class metadata information , Guarantee that there is no non conformity Java Metadata information of language specification . such as : except java.lang.Object Whether there is a parent of a class other than 、 Does it inherit a class that is not allowed to be inherited 、 Whether the non abstract class implements all the methods required to be implemented in its parent class or interface 、 Whether the parent class's final Fields, etc .

Bytecode verification

This stage through data flow and control flow analysis , Make sure the program semantics are legal 、 Logical . such as : Keep the data type consistent when placing and using the operation stack 、 Ensure that the jump instruction does not jump to the bytecode instruction outside the method body 、 To ensure that the type conversion in the method body is effective, etc .

Symbol reference check

This stage is beyond the class itself ( Various symbol references in the constant pool ) Check the match of the information , It happens in analysis In the step , Make sure analysis Can perform normally , such as : Whether the fully qualified name described by string in symbol reference can find the corresponding class 、 Whether the accessibility of the class field method in the symbol reference can access the current class, etc .

Get ready

Last

Now is the climax of the spring move of gold, silver and four , A while ago, Xiaobian has been building his own website , And sorted out a full set of **【 First line internet factory Java Core interview question bank + analysis 】: Include Java Basics 、 abnormal 、 aggregate 、 Concurrent programming 、JVM、Spring Family bucket 、MyBatis、Redis、 database 、 middleware MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty wait **… All have been sorted and uploaded in ** On my Tencent document :  Click here to go to the portal ** And will continue to update … Sure star once .

image

copyright notice
author[Programmer Ning],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210826111228308j.html

Random recommended