jdk8以前的架构
jdk8以后的架构
架构中的属性
- 堆:线程共享;主要存放对象实例(和数组);垃圾收集器的主要管理区域(回收垃圾);堆内存中可以存在物理上不连续的空间,只要逻辑上是连续的即可
- 栈:线程私有;栈中存放的是一个个的栈帧,每个栈帧对应一个被调用的方法(一个方法就是一个栈帧),在栈帧中包括局部变量、操作数、动态链接方法(调用另外 的方法)、方法返回地址和一些额外的信息
- 程序计数器:在多线程情况下,当线程数量超过CPU数量或CPU内核数量时,线程之间就要根据时间片轮询抢夺CPU时间资源。线程之间就会进行切换。因此为了线程切换后能够恢复到正确的执行位置,每条线程都需要一个独立的程序计数器去记录其正在执行的字节码指令地址。
- 本地方法栈(与栈类似):线程私有;一个本地方法就是一个Java调用非Java代码的接口(即Native方法)
- 方法区:线程共享;不需要连续的内存,存储已被虚拟机加载的类信息、常量、静态变量,静态代码块、即时编译器(JIT Compiler)编译后的代码数据等。
- 元空间:同方法区,但存在于JVM外的直接内存中,其中字符串常量池以及静态变量转移到堆当中。
总结:即创建一个对象的时候,对象所代表的类的信息存放在方法区,对象实例存放在堆,对象里面的局部变量、方法等存放在栈
示例
定义如下代码:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return String.format("Student{name='%s', age=%d}", name, age);
}
}
public class test {
public static void main(String[] args) {
Student student = new Student("张三", 10);
student.toString();
}
}
jvm结构
输入javap -v test.class
查看其详细信息
Last modified 2022-3-25; size 661 bytes
MD5 checksum 1876ab678dd84ee7edab8d71455bd3a2
Compiled from "test.java"
public class test
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #9.#25 // java/lang/Object."<init>":()V
#2 = Class #26 // Student
#3 = String #27 // 张三
#4 = Methodref #2.#28 // Student."<init>":(Ljava/lang/String;I)V
#5 = Methodref #2.#29 // Student.toString:()Ljava/lang/String;
#6 = Fieldref #30.#31 // java/lang/System.out:Ljava/io/PrintStream;
#7 = Methodref #32.#33 // java/io/PrintStream.println:(Ljava/lang/String;)V
#8 = Class #34 // test
#9 = Class #35 // java/lang/Object
#10 = Utf8 <init>
#11 = Utf8 ()V
#12 = Utf8 Code
#13 = Utf8 LineNumberTable
#14 = Utf8 LocalVariableTable
#15 = Utf8 this
#16 = Utf8 Ltest;
#17 = Utf8 main
#18 = Utf8 ([Ljava/lang/String;)V
#19 = Utf8 args
#20 = Utf8 [Ljava/lang/String;
#21 = Utf8 student
#22 = Utf8 LStudent;
#23 = Utf8 SourceFile
#24 = Utf8 test.java
#25 = NameAndType #10:#11 // "<init>":()V
#26 = Utf8 Student
#27 = Utf8 张三
#28 = NameAndType #10:#36 // "<init>":(Ljava/lang/String;I)V
#29 = NameAndType #37:#38 // toString:()Ljava/lang/String;
#30 = Class #39 // java/lang/System
#31 = NameAndType #40:#41 // out:Ljava/io/PrintStream;
#32 = Class #42 // java/io/PrintStream
#33 = NameAndType #43:#44 // println:(Ljava/lang/String;)V
#34 = Utf8 test
#35 = Utf8 java/lang/Object
#36 = Utf8 (Ljava/lang/String;I)V
#37 = Utf8 toString
#38 = Utf8 ()Ljava/lang/String;
#39 = Utf8 java/lang/System
#40 = Utf8 out
#41 = Utf8 Ljava/io/PrintStream;
#42 = Utf8 java/io/PrintStream
#43 = Utf8 println
#44 = Utf8 (Ljava/lang/String;)V
{
public test();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Ltest;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=4, locals=2, args_size=1
0: new #2 // class Student
3: dup
4: ldc #3 // String 张三
6: bipush 10
8: invokespecial #4 // Method Student."<init>":(Ljava/lang/String;I)V
11: astore_1
12: aload_1
13: invokevirtual #5 // Method Student.toString:()Ljava/lang/String;
16: pop
17: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
20: aload_1
21: invokevirtual #5 // Method Student.toString:()Ljava/lang/String;
24: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
27: return
LineNumberTable:
line 3: 0
line 4: 12
line 5: 17
line 6: 27
LocalVariableTable:
Start Length Slot Name Signature
0 28 0 args [Ljava/lang/String;
12 16 1 student LStudent;
}
SourceFile: "test.java"