一、JVM整体架构
1.JVM组层
JVM(虚拟机):指以软件的方式模拟具有完整硬件功能、运行在一个完全隔离环境中的完整计算机系统,是物理机的软件实现。
JVM由三个主要的子系统构成
- 类加载子系统
- 运行时数据区(内存结构)
- 执行引擎

2.线程包含的私有内存
程序计数器、Java栈、本地方法栈

class StudyJVM {public static final Integer CONSTANT_1 = 666;public static void main (String [] args) {StudyJVM studyJVM = new StudyJVM();int count = studyJVM.method1();}public int method1() {method2();int a = 1;int b = 2;int count = (a + b) * 10;return count;}public void method2() {System.out.println("end");}
}
Java 栈的运行流程
main方法栈帧先压入java栈。main方法调用method1方法,然后method1方法压入栈帧。method1方法调用method2方法,然后method2方法压入栈。method2执行完出栈,method1执行完出栈,main方法执行完出栈。
3.栈帧
局部变量、操作数栈、动态链接、方法出口

# 将StudyJVM.class 编译成易读的文件到 studyJVM.txt
# 主要功能为分解Java编译生成的class文件,用于查看字节码或进行反编译操作
javap -c StudyJVM.class > studyJVM.txt
Compiled from "StudyJVM.java"
class org.example.StudyJVM {public static final java.lang.Integer CONSTANT_1;org.example.StudyJVM();Code:0: aload_01: invokespecial #1 // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]);Code:0: new #2 // class org/example/StudyJVM3: dup4: invokespecial #3 // Method "<init>":()V7: astore_18: aload_19: invokevirtual #4 // Method method1:()I12: istore_213: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;16: iload_217: invokevirtual #6 // Method java/io/PrintStream.println:(I)V20: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;23: ldc #7 // String end25: invokevirtual #8 // Method java/io/PrintStream.println:(Ljava/lang/String;)V28: returnpublic int method1();Code:0: aload_01: invokevirtual #9 // Method method2:()V4: iconst_15: istore_16: iconst_27: istore_28: iload_19: iload_210: iadd11: bipush 1013: imul14: istore_315: iload_316: ireturnpublic void method2();Code:0: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #10 // String start5: invokevirtual #8 // Method java/io/PrintStream.println:(Ljava/lang/String;)V8: returnstatic {};Code:0: sipush 6663: invokestatic #11 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;6: putstatic #12 // Field CONSTANT_1:Ljava/lang/Integer;9: return
}
public int method1() {method2();int a = 1;int b = 2;int count = (a + b) * 10;return count;}
4: iconst_1
5: istore_1
6: iconst_2
7: istore_2
8: iload_1
9: iload_2
10: iadd
11: bipush 10
13: imul
14: istore_3
15: iload_3
16: ireturn
