当前位置:网站首页>String constant pool, class constant pool, and runtime constant pool
String constant pool, class constant pool, and runtime constant pool
2022-06-26 10:13:00 【ZZ learn java well】
stay java In the memory allocation of , Often hear a lot about Constant pool Description of , When I started to look at it, it was very vague , There are so many different opinions on the Internet , Finally, consult various materials , Finally, it is almost clear , Many online statements have problems , The author tries to distinguish these concepts .
1. overall situation character string pool (string pool Also known as string literal pool)
The contents of the global string pool are completed after class loading , After verification , After the preparation stage Generate string object instances in the heap , Then save the reference value of the string object instance to string pool in ( remember :string pool The reference value is stored in, not the specific instance object , Specific instance objects are stored in a piece of space opened in the heap .). stay HotSpot VM What's achieved in string pool The function is a StringTable class , It's a hash table , There are resident strings ( That is to say, we often use double quotation marks ) References to ( Instead of dwelling on the string instance itself ), That is to say, some string instances in the heap are replaced by this StringTable After the reference is given ” Resident string ” The identity of the . This StringTable At every HotSpot VM There is only one example of , Shared by all classes .
2.class File constant pool (class constant pool)
We all know ,class The file contains only the version of the class 、 Field 、 Method 、 Interface and other description information , Another piece of information is the constant pool (constant pool table), Used to store various literal quantities generated by the compiler (Literal) And symbol reference (Symbolic References). Literal quantity is what we call the concept of constant , Such as text string 、 Be declared final Constant value of . A symbol reference is a set of symbols that describe the target being referenced , A symbol can be any form of literal quantity , As long as it can be used to locate the target unambiguously ( It's different from direct reference , A direct reference is usually a local pointer to a method area , Relative offset or a handle that can be indirectly located to the target ). It generally includes the following three types of constants :
- Fully qualified names of classes and interfaces
- Name and descriptor of the field
- The name and descriptor of the method
Each constant in a constant pool is a table , The total number is as shown in the table below 11 Different table structure data , The first bit at the beginning of each table is the flag bit of a byte ( Value 1-12), Represents which constant type the current constant belongs to .

Each type of constant has a different structure , The specific structure of this article will not be described , This paper focuses on distinguishing these three constant pools ( If you want to know more about the data structure of each constant type, you can see 《 In depth understanding of java virtual machine 》 Content of chapter six ).
3. Runtime constant pool (runtime constant pool)
When java File is compiled into class After the document , That is, it will generate what I said above class Constant pool , When is the runtime constant pool generated ?
jvm When executing a class , Must be loaded 、 Connect 、 initialization , And connection includes verification 、 Get ready 、 Analyze three stages . When the class is loaded into memory ,jvm Will be class The contents of the constant pool are stored in the runtime constant pool , Thus we can see that , The runtime constant pool also has one for each class . I said it on the top ,class The constant pool contains literal quantities and symbolic references , In other words, they don't store instances of objects , It's the symbolic reference value of the object . And after analysis (resolve) after , That is, replace the symbolic reference with the direct reference , The parsing process will query the global string pool , That's what we said above StringTable, To ensure that the strings referenced by the runtime constant pool are consistent with those referenced in the global string pool .
Take an example to illustrate :
public class HelloWorld {
public static void main(String []args) {
String str1 = "abc";
String str2 = new String("def");
String str3 = "abc";
String str4 = str2.intern();
String str5 = "def";
System.out.println(str1 == str3);//true
System.out.println(str2 == str4);//false
System.out.println(str4 == str5);//true
}
}Go back to the program above , Now it's easy to explain the memory allocation process of the whole program , First , There will be one in the heap ”abc” example , overall situation StringTable There is ”abc” A reference value for , Then two instances will be generated when running the second sentence , One is ”def” Instance object of , also StringTable Store a ”def” Reference value , The other one is new The one that came out ”def” Instance object of , It's a different example from the one above , When analyzing str3 Find out when you need to StringTable, There are ”abc” Global resident string reference for , therefore str3 The reference address of is the same as the existing one ,str4 Is called at run time intern() function , return StringTable in ”def” Reference value , If not, it will str2 Add the reference value of , ad locum ,StringTable There is already ”def” Reference value of , So go back up there new str2 Add to StringTable Medium “def” Reference value , Last str5 In the analysis, it also points to the existence of StringTable Medium ”def” Reference value , So after this analysis , The following three printed values are easy to understand . The above program is compiled first , In this category class Some symbol references are stored in the constant pool , And then after the class loads , take class The symbol references stored in the constant pool are transferred to the runtime constant pool , And then it's verified , After the preparation stage , Generate instance objects of resident strings in the heap ( In the above example str1 The point is ”abc” Instance object ), The reference to this object is then saved globally String Pool in , That is to say StringTable in , Finally, in the analysis phase , To replace the symbolic reference in the runtime constant pool with a direct reference , Then direct query StringTable, Guarantee StringTable The reference values in are the same as those in the runtime constant pool , That's probably the whole process .
summary
- 1. The global constant pool is in each VM Only one of them , Store the reference value of string constant .
- 2.class Constant pools are created at compile time class All have , In the compilation phase , It stores symbolic references to constants .
- 3. The runtime constant pool is after the class is loaded , Each one class The symbolic reference values in the constant pool are transferred to the runtime constant pool , in other words , Every class Each has a pool of runtime constants , Class after parsing , Replace symbolic references with direct references , Consistent with the reference values in the global constant pool .
边栏推荐
猜你喜欢

方法区里面有什么——class文件、class文件常量池、运行时常量池

Extracting public fragments from thymeleaf

druid数据源实现后台监控

P1296 whispers of cows (quick row + binary search)

Cloud native essay using Hana expression database service on Google kubernetes cluster

测试须知——常见接口协议解析

What you need to know to test -- URL, weak network, interface, automation

c语言语法基础之——指针(字符、一维数组) 学习

Test instructions - common interface protocol analysis

c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
随机推荐
Tensorflow dynamically allocates video memory
自动化测试——关于unitest与pytest初始化共存问题
Internationalization configuration
Introduction to stored procedure testing
瑞萨电子面向物联网应用推出完整的智能传感器解决方案
定制拦截器
Druid data source for background monitoring
Nested recyclerview in nestedscrollview automatically slides to the bottom after switching
LSP 是什么
Appium自动化测试基础 — 移动端测试环境搭建(二)
SQL function
Threadmode interpretation of eventbus
Standard implementation of streaming layout: a guide to flexboxlayout
微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复
2. 合并两个有序数组
Problems encountered by jupyter notebook
jar版本冲突问题解决
Install new version cmake & swig & tinyspline
Redis notes (14) - persistence and data recovery (data persistence RDB and AOF, data recovery, mixed persistence)
My creation anniversary