JVM virtual machine principle and tuning knowledge (2022)

JVM virtual machine

1. Basics

JVM:JavaVirtualMachine:Java虚拟机

(1) Hierarchical relationship:

	JDK
		JRE
			JVM
				操作系统

(2) Hierarchical functions:

1. Java program → compile → Java bytecode

2. Java bytecode → JVM execution → operating system

	执行java字节码:二进制

3. Operating system → Compile → Hardware

(3) JVM classification

1. Class loading subsystem

2. Runtime data area [stack, heap, method area]

1. Method area

2. Heap: thread sharing, automatic management

散布:散点
堆:存储单位
区分电脑内存堆
存放内容:实例、new对象实例、this指针、数组
管理:垃圾回收器GC自动内存管理机制

3. Stack: sequential queue, automatic management

队列:顺序、连续
栈:运行时单位
区分电脑内存栈
公共基本数据类型:int
java方法——栈帧
压栈(栈帧)——出栈(栈帧)

4. Register: Thread

指令地址
1个线程——1个寄存器

5. Native method stack

native关键标注

3. Execution engine (JIT compiler, interpreter)

JIT编译器(性能):编译执行
解释器(响应时间):逐行解释字节码

(4) JVM architecture

1. class file

入口:编译好字节码文件(编译器前端)

2. Class loading subsystem

经过:类加载子系统(字节码加载到内存生成class对象)
	加载--->链接--->初始化

3. Runtime data area

4. Execution engine

解释器(解释运行)
jit及时编译器(编译器后端)
垃圾回收器三部分

(5) Process

1. Java code execution process

Class文件  →  装载(1) →  类加载器子系统  →  装载(2)  →  运行时数据区
运行时数据区  ↔  执行引擎  ↔  本地方法接口  ↔  本地方法库
运行时数据区  ↔  本地方法接口  ↔  本地方法库

2. Java code compilation and execution process

1. Java bytecode generation process: .class

源代码 →  词法分析器 →  Token流 →  语法分析器 →  语法树/抽象语法树 →  语义分析器 →  注解抽象语法树 →  字节码生成器 →  JVM字节码

2. Bytecode execution: JVM execution engine

JVM字节码 →  机器无关优化 →  中间代码 →  机器相关优化  →  中间代码  →  寄存器分配器  →  中间代码  →  目标代码生成器   →  目标代码

(6) JVM life cycle

(7) JVM operating mode

1. Server mode (heavyweight virtual machine): slow startup

2. Client mode (lightweight virtual machine): fast startup

客户端程序

Second, class loading: ClassLoader + subclass

Bootstrap ClassLoader
Extension ClassLoader
App ClassLoader
Custom ClassLoader

(1) Loading: Loading

类加载到内存
	1通过一个类的全限定名来获取其定义的二进制字节流
	2将这个字节流所代表的的静态存储结构转化为方法区的运行时数据结构
	3在堆中生成一个代表这个类的Class对象,作为方法区中这些数据的访问入口

(2) Connection

1. Verification: class (correctness)

检查类的合法性
	文件格式验证
	元数据验证
	字节码验证
	符号引用验证

2. Preparation: variables (allocation of space)

静态变量赋初始值
成员变量赋设定值

3. Parse: Reference (optional)

符号引用转换为直接引用
常量池内的符号引用替换为直接引用的过程(地址引用)

(3) Initialization

静态变量赋设定值
1、父类的静态变量

2. The static code block of the parent class
3. The static variable
of the child class 4. The static code block of the child class
5. The non-static variable
of the parent class 6. The non-static code block
of the parent class 7. The construction method of the parent class
8. The child Non-static variables
of a class 9, non-static code blocks
of subclasses 10, constructors of subclasses

(4) Use

(5) Uninstall

Third, the class loader

隐式装载:new
显式装载:class.forName
类:系统类、扩展类、自定义类

(1) Classification

1. Start the class loader (bootstrap class loader): Bootstrap ClassLoader)

2. Extension class loader: Extension ClassLoader

3. Program class loader (system class loader): Application ClassLoader

4. Custom class loader: User ClassLoader

(2) Class loading mechanism

1. Take full responsibility

2. Parent class delegation

3. Cache mechanism

(3) Parental delegation model

Fourth, the class implementation:

Five, JVM memory management

(1) Thread private: Thread Local

	程序计数器PC
	栈内存(VM Stack)
		栈帧
		异常
	本地方法栈

(2) Thread sharing: Thread Shared

1. Method area (permanent generation)

	运行时常量池

2, heap memory (Heap)

	存储对象或数组
	新生代
	老年代
	异常

(3) Direct memory: Direct Memory

不受JVM GC管理

6. Thread

(1) Guardian thread

	gc(垃圾回收机制)
	匿名守护线程

(2) Non-daemon threads (ordinary threads):

	main函数——java应用——java虚拟机
	main函数1——java应用1——java虚拟机1
	main函数2——java应用2——java虚拟机2

Guess you like

Origin blog.csdn.net/qq_25482375/article/details/124331336