当前位置:网站首页>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
}
边栏推荐
- Methods to detect whether PHP websites have been broken
- Watson K's Secret Diary
- [激光器原理与应用-2]:国内激光器重点品牌
- 店铺门面转让出租小程序开发制作功能介绍
- Matter protocol
- Several time synchronization methods of Beidou timing system (GPS timing equipment)
- Statistical knowledge required by data analysts
- Sslstrip Ultimate - location hijacking
- B - wall painting (C language)
- 三层带防护内网红队靶场
猜你喜欢

Google 有哪些牛逼的开源项目?

Eth relay interface

Source insight 4.0 setting shortcut keys for comments and uncomments

MySQL锁总结

From the first generation of sowing to the first generation of flowers, 5g commercial "gave birth to all things" for the third anniversary

7. 列表标签

Guanghetong LTE Cat4 module l716 is upgraded to provide affordable and universal wireless applications for the IOT industry

This artifact is highly recommended. One line command will convert the web page to PDF!

7. list label

零时科技 | Discover 闪电贷攻击事件分析
随机推荐
游戏数学: 计算屏幕点中的平面上的点(上帝视角)
FreeRTOS startup - based on stm32
Esp32 development -lvgl uses internal and external fonts
Vulkan-官方示例解读-Shadows(光栅化)
Eth Of Erc20 And Erc721
再聊数据中心网络
Market prospect analysis and Research Report of welding laser in 2022
域名解析耗时是什么?域名解析耗时影响因素有哪些?
你知道MallBook分账与银行分账的区别吗?
Rational use of thread pool and thread variables
Market prospect analysis and Research Report of pipe and hose press fitting tools in 2022
golang泛型:generics
What great open source projects does Google have?
Construction of esp8266/esp32 development environment
Image detection related model data format
[pan micro E9 development] single sign on Kingdee eas
Summary of C language implementation of BP neural network
Embedded basic interface-i2c
Grandpa simayan told you what is called inside roll!
从初代播种到落地生花,5G商用三周年“催生万物”