当前位置:网站首页>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 .
边栏推荐
- Omni channel, multi scenario and cross platform, how does app analyze channel traffic with data
- Logical English structure [key points]
- Introduction to stored procedure testing
- Allocation of heap memory when creating objects
- Force buckle ----- remove the maximum and minimum values from the array
- Nested recyclerview in nestedscrollview automatically slides to the bottom after switching
- Problems encountered by jupyter notebook
- The basis of C language grammar -- function definition learning
- 动态库连接 - 符号冲突 - 全局符号介入
- 自动化测试——pytest本身及第三方模块介绍及使用
猜你喜欢
904. 水果成篮
118. Yanghui triangle
c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
DAY 3 数组,前置后置,字符空间,关键词和地址指针
A list of common methods for customizing paint and canvas of view
Test instructions - common interface protocol analysis
What you need to know to test -- URL, weak network, interface, automation
cmake / set 命令
118. 杨辉三角
創建對象的時候堆內存的分配
随机推荐
mysql学习总结
Get the clicked position in the recyclerview
Cloud native essay using Hana expression database service on Google kubernetes cluster
Redis master-slave replication in win10 system
从tf 1.x到tf 2.6(遇到的就过来更新更新)
Does the go compiled executable have dynamic library links?
爬虫相关文章收藏:pyppeteer 、Burpsuite
Download MySQL database installation package website of each system and version
全渠道、多场景、跨平台,App如何借助数据分析渠道流量
The basis of C language grammar -- pointer (multidimensional array, function, summary) learning
测试实践——app 测试注意点
瑞萨电子面向物联网应用推出完整的智能传感器解决方案
Full introduction to flexboxlayout (Google official flexible implementation of flow layout control)
Specific implementation comparison between different programming languages
904. fruit baskets
The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions
Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)
US President signs community safety act to deal with gun issue
1. sum of two numbers (leetcode topic)
904. 水果成篮