下面分析一下元空间、堆、线程在内存中的联系
以下面代码为例
public class HelloWorld {
private String name;
public void sayHello() {
System.out.println(name);
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
int a = 1;
HelloWorld hw = new HelloWorld();
hw.setName("test");
hw.sayHello();
}
}
让我们来反编译下HelloWorld.class文件
C:\Users\Administrator\Desktop>javac HelloWorld.java
C:\Users\Administrator\Desktop>javap -verbose HelloWorld
Classfile /C:/Users/Administrator/Desktop/HelloWorld.class
Last modified 2019-8-18; size 635 bytes
MD5 checksum cf6b1e8b7a93a33de46deafdde7503c9
Compiled from "HelloWorld.java"
public class HelloWorld
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #10.#24 // java/lang/Object."<init>":()V
#2 = Fieldref #25.#26 // java/lang/System.out:Ljava/io/PrintStream;
#3 = Fieldref #5.#27 // HelloWorld.name:Ljava/lang/String;
#4 = Methodref #28.#29 // java/io/PrintStream.println:(Ljava/lang/String;)V
#5 = Class #30 // HelloWorld
#6 = Methodref #5.#24 // HelloWorld."<init>":()V
#7 = String #31 // haha
#8 = Methodref #5.#32 // HelloWorld.setName:(Ljava/lang/String;)V
#9 = Methodref #5.#33 // HelloWorld.sayHello:()V
#10 = Class #34 // java/lang/Object
#11 = Utf8 name
#12 = Utf8 Ljava/lang/String;
#13 = Utf8 <init>
#14 = Utf8 ()V
#15 = Utf8 Code
#16 = Utf8 LineNumberTable
#17 = Utf8 sayHello
#18 = Utf8 setName
#19 = Utf8 (Ljava/lang/String;)V
#20 = Utf8 main
#21 = Utf8 ([Ljava/lang/String;)V
#22 = Utf8 SourceFile
#23 = Utf8 HelloWorld.java
#24 = NameAndType #13:#14 // "<init>":()V
#25 = Class #35 // java/lang/System
#26 = NameAndType #36:#37 // out:Ljava/io/PrintStream;
#27 = NameAndType #11:#12 // name:Ljava/lang/String;
#28 = Class #38 // java/io/PrintStream
#29 = NameAndType #39:#19 // println:(Ljava/lang/String;)V
#30 = Utf8 HelloWorld
#31 = Utf8 haha
#32 = NameAndType #18:#19 // setName:(Ljava/lang/String;)V
#33 = NameAndType #17:#14 // sayHello:()V
#34 = Utf8 java/lang/Object
#35 = Utf8 java/lang/System
#36 = Utf8 out
#37 = Utf8 Ljava/io/PrintStream;
#38 = Utf8 java/io/PrintStream
#39 = Utf8 println
{
public HelloWorld();
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
public void sayHello();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream
3: aload_0
4: getfield #3 // Field name:Ljava/lang/String;
7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/
10: return
LineNumberTable:
line 6: 0
line 7: 10
public void setName(java.lang.String);
descriptor: (Ljava/lang/String;)V
flags: ACC_PUBLIC
Code:
stack=2, locals=2, args_size=2
0: aload_0
1: aload_1
2: putfield #3 // Field name:Ljava/lang/String;
5: return
LineNumberTable:
line 10: 0
line 11: 5
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=1
0: iconst_1
1: istore_1
2: new #5 // class HelloWorld
5: dup
6: invokespecial #6 // Method "<init>":()V
9: astore_2
10: aload_2
11: ldc #7 // String haha
13: invokevirtual #8 // Method setName:(Ljava/lang/String;)V
16: aload_2
17: invokevirtual #9 // Method sayHello:()V
20: return
LineNumberTable:
line 14: 0
line 15: 2
line 16: 10
line 17: 16
line 18: 20
}
SourceFile: "HelloWorld.java"