当前位置:网站首页>JVM class loading process
JVM class loading process
2022-06-11 11:29:00 【Ther233】
Pre knowledge
The life cycle of a class :
1. load
2. Connect
2.1 verification
2.2 Get ready
2.3 analysis
3. initialization
4. Use
5. uninstall
Class loading process
There are three steps in the class loading process :
1. load
2. Connect
2.1 verification
2.2 Get ready
2.3 analysis
3. initialization
load
First step of class loading process , load , I did three things :
1. Get the bytecode file of the class through the full class name , And load it into a binary byte stream
2. The static storage structure represented by the byte stream ( Coding structure ) Convert to the runtime data structure of the method area ( Memory structure at run time )
3. Generate an in memory... That represents the class Class object , As the access to the data in the method area
package com.tan.classloading;
// Static storage structure
public class Cola {
private int price =2;
private static int capacity = 330;
static {
int invent_date = 18860508;
}
public Cola(){
price = 3;
price = 5;
}
}
// By looking at the bytecode file , Observe the memory structure of the runtime
Classfile /D:/220106/code/Jvm/target/classes/com/tan/classloading/Cola.class
Last modified 2022-6-10; size 449 bytes
MD5 checksum e6d9a0cd5658f8b7f42c72225c62fbbe
Compiled from "Cola.java"
public class com.tan.classloading.Cola
minor version: 0
major version: 49
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #6.#20 // java/lang/Object."<init>":()V
#2 = Fieldref #5.#21 // com/tan/classloading/Cola.price:I
#3 = Fieldref #5.#22 // com/tan/classloading/Cola.capacity:I
#4 = Integer 18860508
#5 = Class #23 // com/tan/classloading/Cola
#6 = Class #24 // java/lang/Object
#7 = Utf8 price
#8 = Utf8 I
#9 = Utf8 capacity
#10 = Utf8 <init>
#11 = Utf8 ()V
#12 = Utf8 Code
#13 = Utf8 LineNumberTable
#14 = Utf8 LocalVariableTable
#15 = Utf8 this
#16 = Utf8 Lcom/tan/classloading/Cola;
#17 = Utf8 <clinit>
#18 = Utf8 SourceFile
#19 = Utf8 Cola.java
#20 = NameAndType #10:#11 // "<init>":()V
#21 = NameAndType #7:#8 // price:I
#22 = NameAndType #9:#8 // capacity:I
#23 = Utf8 com/tan/classloading/Cola
#24 = Utf8 java/lang/Object
{
public com.tan.classloading.Cola();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_2
6: putfield #2 // Field price:I
9: aload_0
10: iconst_3
11: putfield #2 // Field price:I
14: aload_0
15: iconst_5
16: putfield #2 // Field price:I
19: return
LineNumberTable:
line 9: 0
line 4: 4
line 10: 9
line 11: 14
line 12: 19
LocalVariableTable:
Start Length Slot Name Signature
0 20 0 this Lcom/tan/classloading/Cola;
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=1, args_size=0
0: sipush 330
3: putstatic #3 // Field capacity:I
6: ldc #4 // int 18860508
8: istore_0
9: return
LineNumberTable:
line 5: 0
line 7: 6
line 8: 9
LocalVariableTable:
Start Length Slot Name Signature
}
SourceFile: "Cola.java"
How to view the memory structure of runtime through bytecode file ?
First click on Build-Build project, Compile this class , To generate the bytecode file corresponding to this class
Secondly, in target The bytecode file is found under the folder , The right choice open in terminal
Last in terminal Enter command in :
Show... On the command line :
javap -v Cola.class
Store the bytecode file in the specified file :
javap -v Cola.class >a.txt
In order to better observe the memory structure of the runtime , Can be in idea Download plugins from :
jclasslib Bytecode Viewer
How do you use it? jclasslib Bytecode Viewer To view the run-time memory structure of bytecode files ?
Click on View-show bytecode with jclasslib
Connect
The second step of the class loading process , Connect , It can be subdivided into three steps :
1. verification
The purpose is to ensure c1ass The byte stream of the file contains information that meets the requirements of the current virtual machine , Ensure the correctness of the loaded class , Does not harm the security of virtual machine itself
It mainly includes four kinds of verification , File format validation , Metadata validation , Bytecode verification , Symbol reference validation
--> Carry out various verifications , To ensure that the class is loaded correctly
2. Get ready
Allocate memory for a class variable and set the default initial value of the class variable , That's zero
There is no use for final Embellished static, because final Assigned at compile time , The preparation phase explicitly initializes
There is no initialization assigned to instance variables , Class variables are assigned in the method area , And instance variables are assigned to along with the object Java In the pile
--> Class variables (static Decorated variable , barring final static Embellished , because final Assigned at compile time ) Allocate memory , And set the default initial value of the class variable
3. analysis
To convert a symbolic reference in a constant pool into a direct procedure
in fact , Analytic substitution is often accompanied by M Do not execute... Until initialization is complete
Symbol reference is a set of symbols to describe the referenced target . The literal form of symbolic reference is clearly defined in 《Java Virtual machine model 》 Of c1ss In file format . A direct reference is a pointer directly to the target 、 Relative offset or an indirect handle to the target
Parsing actions are mainly for classes or interfaces 、 Field 、 Class method 、 Interface method 、 Method type, etc . Corresponds to... In the constant pool CONSTANT_Class_info,CONSTANT_Fieldref_info,CONSTANT_Methodref_info etc.
--> take Java Class resolves to various components : Constant pool 、 Interface 、 Field 、 Method 、 Properties, etc
Be careful :
The loading phase and the connection phase are not strictly pre - and post - execution , Part of the load phase and the connect phase are interleaved , The loading phase may not be over yet , The connection phase may have started
initialization
The third step of the class loading process , initialization :
The initialization phase is the execution of class constructor methods <c11n1t() The process of
This method does not need to be defined , yes javac The compiler automatically collects the assignment actions of all class variables in the class and the statements in the static code block
The instructions in the constructor method are executed in the order in which the statements appear in the source file
<c11n1t>() Constructor different from class .( relation : The constructor is from the perspective of virtual machine <init>())
If the class has a parent class ,JVM Will guarantee the subclass <c1init>() Before execution , Of the parent class <c1init>() Execution completed
The virtual machine must guarantee a class of <c1init>() Method is locked synchronously under multithreading
--> Execute class constructor <init> And initialization methods <clinit> The process of
--><clinit> It's not a constructor , It mainly aggregates all static variable assignments and static code blocks , And assign a value to it
Be careful :
<clinit> Methods are automatically generated after compilation , also <clinit> The way to do this is to lock threads , Class initialization in a multithreaded environment may cause multiple processes to block
JVM There are six scenarios in which classes must be initialized :
1. encounter new( Create objects ) 、 getstatic( Read as final Static variables of the modified class )、putstatic( Assign values to static variables of a class ) or invokestatic( Call a static method in a class ) this 4 Direct code instructions
e.g: Create objects
2. Use java.lang.reflect Package's method makes a reflection call on the class
e.g:Class.forname("xxx")、newInstance()
3. Initialize a class , It inherits a class , And its parent class has not been initialized , The initialization of the parent class will be triggered first
4. When the virtual machine starts running , The virtual opportunity initializes the main class to be executed first ( Program entrance )
5. To use MethodHandle and VarHandle ( Lightweight reflection call mechanism ), You have to use it first findStaticVarHandle To initialize the class to be called
6. When an interface defines JDK8 New default method added , namely : By default Keyword decorated interface method , If the implementation class of the interface is initialized , The interface must be initialized before it
边栏推荐
- The complete manual of the strongest Flink operator is a good choice for the interview~
- 迭代器模式--沙场秋点兵
- Use yolov5 to train your own data set and get started quickly
- 【C语言】anonymous/unnamed struct&&union
- Don't be a fake worker
- 【碎碎念】关于波长|波速|周期的想法
- WordPress database cache plug-in: DB cache Reloaded
- 全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据
- 在毕设中学习03
- [issue 30] shopee golang development experience
猜你喜欢

Typeerror: argument of type "Int 'is not Iterable

Method of converting VOC format data set to Yolo format data set

设置默认收货地址【项目 商城】

Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO

Use pydub to modify the bit rate of the wav file, and an error is reported: c:\programdata\anaconda3\lib\site packages\pydub\utils py:170: RuntimeWarning:

适配器模式--能不能好好说话?

Don't be a fake worker

JS prototype. The find () method has no effect on the object array. It is urgent...

Problems encountered when using nailing intranet to penetrate and upload PHP projects

Cap theory sounds very big, but it's actually very simple
随机推荐
李飞飞:我更像物理学界的科学家,而不是工程师|深度学习崛起十年
Only when you find your own advantages can you work tirelessly and get twice the result with half the effort!
收货地址列表展示【项目 商城】
Learning in Bi design 03
Use yolov5 to train your own data set and get started quickly
Summary of information of main account of Chia Tai futures on Wednesday in advance
MWC 2022 lights up the future, and everything serves
985 University doctors became popular because of their thanks in classical Chinese! The tutor commented that he not only wrote well in sci
Digital collection system app source code
WordPress数据库缓存插件:DB Cache Reloaded
Définir l'adresse de réception par défaut [Centre commercial du projet]
js合并两个对象(面试题)
UCI-HAR数据集的处理
JS merge two objects (interview questions)
How to understand CPU load
WordPress用户名修改插件:Username Changer
nft数字藏品app系统搭建
Is it safe for Xiaobai to open an account directly on the flush?
使用Yolov5训练自己制作的数据集,快速上手
Count the top k strings with the most occurrences