当前位置:网站首页>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
}
边栏推荐
- Use tool classes to read Excel files according to certain rules
- Feature selection algorithm based on bare bones particleswarm optimization
- FreeRTOS startup - based on stm32
- Programming battle -- challenging college entrance examination questions
- Market prospect analysis and Research Report of marking laser in 2022
- 详解 | 晶振的构造及工作原理
- Unity prefab scene 冲突合并工具 UnityYAMLMerge
- Zhongang Mining: fluorochemical industry is the main consumption field of fluorite
- JVM(3):类加载器分类、双亲委派机制
- How to invest in programming knowledge and reduce the impact of knowledge failure
猜你喜欢

Introduction to the development and production functions of shop facade transfer and rental applet

It's 2022. When will the "module freedom" be realized?

你知道MallBook分账与银行分账的区别吗?

JVM(4):类的主动使用与被动使用、运行时数据区域内部结构、JVM线程说明、PC寄存器

再聊数据中心网络

Source Insight 4.0设置注释与反注释的快捷键

MySQL锁总结

JVM(6):Slot变量槽、操作数栈、代码追踪、栈顶缓存技术

Pictures that make people feel calm and warm

零时科技 | Discover 闪电贷攻击事件分析
随机推荐
Protection and bypass of repeated contracting
ESP series module burning firmware
Composition and configuration of GPS Beidou clock synchronization (satellite time synchronization system) in power plant
2022爱分析· 隐私计算厂商全景报告 | 爱分析报告
编程大作战 -- 挑战高考题
Use tool classes to read Excel files according to certain rules
Feature selection algorithm based on bare bones particleswarm optimization
Some differences between people
Seven easy-to-use decorators
Ultra simple cameraX face recognition effect package
三层带防护内网红队靶场
golang泛型:generics
A Security Analysis Of Browser Extensions
【服务器数据恢复】同友存储raid5崩溃的数据恢复案例
Esp32 development -lvgl uses internal and external fonts
给你一个项目,你将如何开展性能测试工作?
Explain in detail the structure and working principle of the crystal oscillator
从初代播种到落地生花,5G商用三周年“催生万物”
Embedded basic interface SDIO
数据分析师必知必会的统计学知识