当前位置:网站首页>JVM (2): loading process of memory structure and classes
JVM (2): loading process of memory structure and classes
2022-06-11 04:16:00 【Prince you】
One 、 Overview of memory structure

Classloader subsystem

- class After the file enters the virtual machine , First of all to Loading( load ) then Linking( link ) Finally, initialize .
- The loaders used for loading are divided into :Bootstrap ClassLoader( Boot class loader )、Extension ClassLoader( Extend the classloader )、Application ClassLoader( Apply the class loader ) .
- The connection is divided into :Verify( verification )、Prepare( Get ready )、Resolve( analysis ).
Memory area

- PC Registers(PC Register or program counter , The thread has an exclusive share )
- Stack Area( Stack area , The thread has an exclusive share )
- Native Method Stack( Native Method Stack ) Main scope local method interface api call
- Heap Area( Heap area ) Main storage objects
- Method Area( Method area ) The main storage constant 、 Domain 、 Method information, etc .HotSpot Virtual machine specific .
Execution engine

Two 、 Class loader and class loading process

- The classloader subsystem is responsible for loading from the file system or network class file ,class The file has a specific file ID at the beginning of the file .
- . classLoader Only responsible for class Loading of files , As for whether it can run , By ExecutionEngine decision .
- · The loaded class information is stored in a block of memory called the method area . In addition to class information , The method area also holds runtime constant pool information , It may also include string literals and numeric constants ( This part of the constant information is class Memory mapping for the constant pool part of the file )
Such as below :


3、 ... and 、 Class loading process (Loading)

load :
- Get the binary byte stream that defines a class by its fully qualified name
- Convert the static storage structure represented by the byte stream into the runtime data structure of the method area
- Generate a representation of this class in memory java.lang.Class object , As the access to all kinds of data of this class in method area
Add : load .class How to file
- Load directly from the local system
- Get... Through the Internet , Typical scenario : web Applet
- from zip Read in compressed package , Become the future jar、war The basis of format
- Runtime compute generation , The most used is : Dynamic proxy technique
- Generated from other files , Typical scenario :JSP application
- Extract... From a proprietary database .class file , Relatively rare
- Get... From the encrypted file , Typical defense class The protection of the file against decompilation
Four 、 Class loading process (Linking)

verification (Verify)
stay idea Install the binary view plug-in in .
stay class Right click the file to view the binary, and you will find all java The starting positions of bytecode files are CAFEBABE( magic number ) , This is the process of verification .

Get ready (Prepare)
public class HelloApp {
private static int a = 1; // Prepare link a=0 -> initial a The assignment is 1
}5、 ... and 、 Class loading process (initialization)
initialization :
- The initialization phase is the execution of class constructor methods <clinit>() 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 .
- <clinit>() 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 <clinit>() Before execution , Of the parent class <clinit>() Execution completed .
- The virtual machine must guarantee a class of <clinit> () Method is locked synchronously under multithreading .
install BytecodeViewer Plug in view bytecode file .

example 1: java Code :
public class HelloApp {
private static int a = 1;
static {
a = 2;
b = 20;
}
private static int b = 10;
}Select the to view class File find the following options

You can find clinit Static code is initialized in top-down order . Variable b stay prepare Phase assignment is 0, Then the static code block executed first in the initialization phase b=20, Then the following code is executed b=10.
// class version 55.0 (55)
// access flags 0x21
public class org/ywz/springbootdemo/HelloApp {
// compiled from: HelloApp.java
// access flags 0xA
private static I a
// access flags 0xA
private static I b
// access flags 0x1
public <init>()V
L0
LINENUMBER 3 L0
ALOAD 0
INVOKESPECIAL java/lang/Object.<init> ()V
RETURN
L1
LOCALVARIABLE this Lorg/ywz/springbootdemo/HelloApp; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
// access flags 0x8
static <clinit>()V
L0
LINENUMBER 4 L0
ICONST_1
PUTSTATIC org/ywz/springbootdemo/HelloApp.a : I
L1
LINENUMBER 7 L1
ICONST_2
PUTSTATIC org/ywz/springbootdemo/HelloApp.a : I
L2
LINENUMBER 8 L2
BIPUSH 20
PUTSTATIC org/ywz/springbootdemo/HelloApp.b : I
L3
LINENUMBER 10 L3
BIPUSH 10
PUTSTATIC org/ywz/springbootdemo/HelloApp.b : I
RETURN
MAXSTACK = 1
MAXLOCALS = 0
}
example 2:java Code
public class HelloApp {
private int a = 1;
public static void main(String[] args) {
int b = 2;
}
}You can find that when there are no static variables , The bytecode is gone clinit Class constructor method .init Methods are actually constructors .
// class version 55.0 (55)
// access flags 0x21
public class org/ywz/springbootdemo/HelloApp {
// compiled from: HelloApp.java
// access flags 0x2
private I a
// access flags 0x1
public <init>()V
L0
LINENUMBER 3 L0
ALOAD 0
INVOKESPECIAL java/lang/Object.<init> ()V
L1
LINENUMBER 4 L1
ALOAD 0
ICONST_1
PUTFIELD org/ywz/springbootdemo/HelloApp.a : I
RETURN
L2
LOCALVARIABLE this Lorg/ywz/springbootdemo/HelloApp; L0 L2 0
MAXSTACK = 2
MAXLOCALS = 1
// access flags 0x9
public static main([Ljava/lang/String;)V
// parameter args
L0
LINENUMBER 7 L0
ICONST_2
ISTORE 1
L1
LINENUMBER 8 L1
RETURN
L2
LOCALVARIABLE args [Ljava/lang/String; L0 L2 0
LOCALVARIABLE b I L1 L2 1
MAXSTACK = 1
MAXLOCALS = 2
}
example 3:java Code
public class HelloApp {
static class Father {
public static int a = 1;
static {
a = 2;
}
}
static class Son extends Father {
public static int b = a;
}
public static void main(String[] args) {
System.out.println(Son.b);
}
}You can find that the constructor initializes the parent class first and then the child class .
// class version 55.0 (55)
// access flags 0x20
class org/ywz/springbootdemo/HelloApp$Son extends org/ywz/springbootdemo/HelloApp$Father {
// compiled from: HelloApp.java
NESTHOST org/ywz/springbootdemo/HelloApp
// access flags 0x8
static INNERCLASS org/ywz/springbootdemo/HelloApp$Son org/ywz/springbootdemo/HelloApp Son
// access flags 0x8
static INNERCLASS org/ywz/springbootdemo/HelloApp$Father org/ywz/springbootdemo/HelloApp Father
// access flags 0x9
public static I b
// access flags 0x0
<init>()V
L0
LINENUMBER 12 L0
ALOAD 0
INVOKESPECIAL org/ywz/springbootdemo/HelloApp$Father.<init> ()V
RETURN
L1
LOCALVARIABLE this Lorg/ywz/springbootdemo/HelloApp$Son; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
// access flags 0x8
static <clinit>()V
L0
LINENUMBER 13 L0
GETSTATIC org/ywz/springbootdemo/HelloApp$Son.a : I
PUTSTATIC org/ywz/springbootdemo/HelloApp$Son.b : I
RETURN
MAXSTACK = 1
MAXLOCALS = 0
}
边栏推荐
- Esp32 development -lvgl display picture
- [激光器原理与应用-2]:国内激光器重点品牌
- 【CustomView】Glide+BitmapTransformation 图片上下边框波浪处理(WaveTransformation)
- How PTP helps several major operators meet 5g timing requirements
- clickjacking漏洞的挖掘与利用
- Seven easy-to-use decorators
- Eth Transfer
- 超简单 CameraX 人脸识别效果封装
- 检测php网站是否已经被攻破的方法
- Guanghetong LTE Cat4 module l716 is upgraded to provide affordable and universal wireless applications for the IOT industry
猜你喜欢

JVM(5):虚拟机栈、栈异常、栈的存储结果和运行原理、栈内部结构、局部变量表

Introduction to the development and production functions of shop facade transfer and rental applet
![[laser principle and application-2]: key domestic laser brands](/img/55/a87169bb75429f323159e3b8627cc6.jpg)
[laser principle and application-2]: key domestic laser brands

Ultra simple cameraX face recognition effect package

SSLStrip 终极版 —— location 劫持

【网络篇】套接字编程

写给通信年轻人的27个忠告

详解 | 晶振的构造及工作原理

Fundamentals of embedded audio processing

数据分析师必知必会的统计学知识
随机推荐
QT日志模块的个性化使用
域名解析耗时是什么?域名解析耗时影响因素有哪些?
邪恶的CSRF
Market prospect analysis and Research Report of digital line scan camera in 2022
软件工程笔记之期末复习(简答)
Market prospect analysis and Research Report of hydrogen liquefier in 2022
Rational use of thread pool and thread variables
What is the time-consuming domain name resolution? What are the influencing factors of domain name resolution time?
Ultra simple cameraX face recognition effect package
GPS Beidou time service, NTP makes network clock synchronization more accurate
Unity prefab scene 冲突合并工具 UnityYAMLMerge
7. list label
SQL注入关联分析
Market prospect analysis and Research Report of surround packing machine in 2022
从初代播种到落地生花,5G商用三周年“催生万物”
司马炎爷爷 告诉你什么叫做内卷!
Eth Of Erc20 And Erc721
密码找回功能可能存在的问题(补充)
Market prospect analysis and Research Report of modular lithium ion battery in 2022
How PTP helps several major operators meet 5g timing requirements