JVM内存模型—常量池(Method Area)
方法区(Method Area)
- 与堆一样,都是线程共享的,需考虑线程安全问题
- 主要存放与类结构相关的信息,成员变量,方法数据以及成员方法/构造器方法的代码等等
- 在虚拟机启动时被创建
- 逻辑上是堆的组成部分(根据不同的JVM厂商的实现)
- Hospot 1.8以前-永久代:使用堆内存的一部分作为方法区
- hospot 1.8及以后-元空间:使用操作系统内存作为方法区
-
常量池存在于方法区中
class文件常量池
反编译ConstPool类的字节码文件:
使用:
javap -v D:\idea\workspace\OOOO\out\production\OOOO\jianceMemory.class
public class ConstPool {
public ConstPool() {
}
public static void main(String[] args) {
System.out.println("哈哈");
}
}
//反编译输出结果
D:\idea\workspace\OOOO>javap -v D:\idea\workspace\OOOO\out\production\OOOO\ConstPool.class
//类的基本信息
Classfile /D:/idea/workspace/OOOO/out/production/OOOO/ConstPool.class
Last modified 2020-3-11; size 525 bytes
MD5 checksum 346a4b0db3b963d447538633d1c6f1e1
Compiled from "ConstPool.java"
public class ConstPool
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
//class常量池信息
Constant pool:
#1 = Methodref #6.#20 // java/lang/Object."<init>":()V
#2 = Fieldref #21.#22 // java/lang/System.out:Ljava/io/PrintStream;
#3 = String #23 // 哈哈
#4 = Methodref #24.#25 // java/io/PrintStream.println:(Ljava/lang/String;)V
#5 = Class #26 // ConstPool
#6 = Class #27 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 LConstPool;
#14 = Utf8 main
#15 = Utf8 ([Ljava/lang/String;)V
#16 = Utf8 args
#17 = Utf8 [Ljava/lang/String;
#18 = Utf8 SourceFile
#19 = Utf8 ConstPool.java
#20 = NameAndType #7:#8 // "<init>":()V
#21 = Class #28 // java/lang/System
#22 = NameAndType #29:#30 // out:Ljava/io/PrintStream;
#23 = Utf8 哈哈
#24 = Class #31 // java/io/PrintStream
#25 = NameAndType #32:#33 // println:(Ljava/lang/String;)V
#26 = Utf8 ConstPool
#27 = Utf8 java/lang/Object
#28 = Utf8 java/lang/System
#29 = Utf8 out
#30 = Utf8 Ljava/io/PrintStream;
#31 = Utf8 java/io/PrintStream
#32 = Utf8 println
#33 = Utf8 (Ljava/lang/String;)V
{
//类方法定义
public ConstPool();
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 7: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LConstPool;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=1, args_size=1
//虚拟机指令
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String 哈哈
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String
;)V
8: return
LineNumberTable:
line 9: 0
line 10: 8
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 args [Ljava/lang/String;
}
SourceFile: "ConstPool.java"
一个类的字节码包括类的基本信息,常量池,虚拟机指令
常量池就是一张表,虚拟机指令根据这张表查询对应要执行的类名,方法名,参数类型,字面量等信息。
这里单独把虚拟机指令和常量池拿出来处理:
//虚拟机指令
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String 哈哈
5: invokevirtual #4
//常量池
Constant pool:
#1 = Methodref #6.#20 // java/lang/Object."<init>":()V
#2 = Fieldref #21.#22 // java/lang/System.out:Ljava/io/PrintStream;
#3 = String #23 // 哈哈
#4 = Methodref #24.#25 // java/io/PrintStream.println:(Ljava/lang/String;)V
#5 = Class #26 // ConstPool
#6 = Class #27 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 LConstPool;
#14 = Utf8 main
#15 = Utf8 ([Ljava/lang/String;)V
#16 = Utf8 args
#17 = Utf8 [Ljava/lang/String;
#18 = Utf8 SourceFile
#19 = Utf8 ConstPool.java
#20 = NameAndType #7:#8 // "<init>":()V
#21 = Class #28 // java/lang/System
#22 = NameAndType #29:#30 // out:Ljava/io/PrintStream;
#23 = Utf8 哈哈
#24 = Class #31 // java/io/PrintStream
#25 = NameAndType #32:#33 // println:(Ljava/lang/String;)V
#26 = Utf8 ConstPool
#27 = Utf8 java/lang/Object
#28 = Utf8 java/lang/System
#29 = Utf8 out
#30 = Utf8 Ljava/io/PrintStream;
#31 = Utf8 java/io/PrintStream
#32 = Utf8 println
#33 = Utf8 (Ljava/lang/String;)V
解释器在解释中会查表翻译虚拟机指令
比如:
getstatic #2 找到Constant pool中对应的 #2 = Fieldref #21.#22
:引用了成员变量,继续往后找#21,#22
#21 = Class #28
:表示引用一个类的成员变量,继续找#28
#28 = Utf8 java/lang/System
:表示引用的成员变量所在的类名称为java/lang/System,#21这里结束,继续找#22
#22 = NameAndType #29:#30
:引用了#29:#30,找到#29
#29 = Utf8 out
:表示要引用的变量名为Out,继续找#30
#30 = Utf8 Ljava/io/PrintStream;
:表示Out类型为java/io/PrintStream;
运行时常量池
class常量池时.class文件中的,当该类被加载时,会将该类的常量池信息放入内存中,内存中的这个位置叫做运行时常量池,并将符号地址改为真实内存中地址(上方的#2,3,4)