Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
cppg
toBeTopJavaer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
java-basic
/
Runtime-Constant-Pool.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
37 lines (19 loc) · 2.44 KB
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
java-basic
/
Runtime-Constant-Pool.md
Copy path
Top
File metadata and controls
Code
Blame
37 lines (19 loc) · 2.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
运行时常量池
(
Runtime
Constant
Pool
)
是每一个类或接口的常量池
(
Constant_Pool
)
的运行时表示形式
。
它包括了若干种不同的常量
:
从编译期可知的数值字面量到必须运行期解析后才能获得的方法或字段引用
。
运行时常量池扮演了类似传统语言中符号表
(
SymbolTable
)
的角色
,
不过它存储数据范围比通常意义上的符号表要更为广泛
。
每一个运行时常量池都分配在
Java
虚拟机的方法区之中
,
在类和接口被加载到虚拟机后
,
对应的运行时常量池就被创建出来
。
以上
,
是Java虚拟机规范中关于运行时常量池的定义
。
###
运行时常量池在JDK各个版本中的实现
根据Java虚拟机规范约定
:
每一个运行时常量池都在Java虚拟机的方法区中分配
,
在加载类和接口到虚拟机后
,
就创建对应的运行时常量池
。
在不同版本的JDK中
,
运行时常量池所处的位置也不一样
。
以HotSpot为例
:
在JDK
1.7
之前
,
方法区位于堆内存的永久代中
,
运行时常量池作为方法区的一部分
,
也处于永久代中
。
因为使用永久代实现方法区可能导致内存泄露问题
,
所以
,
从JDK1
.7
开始
,
JVM尝试解决这一问题
,
在1
.7
中
,
将原本位于永久代中的运行时常量池移动到堆内存中
。(
永久代在JDK
1.7
并没有完全移除
,
只是原来方法区中的运行时常量池
、
类的静态变量等移动到了堆内存中
。)
在JDK
1.8
中
,
彻底移除了永久代
,
方法区通过元空间的方式实现
。
随之
,
运行时常量池也在元空间中实现
。
###
运行时常量池中常量的来源
运行时常量池中包含了若干种不同的常量
:
编译期可知的字面量和符号引用
(
来自Class常量池
)
运行期解析后可获得的常量
(
如String的intern方法
)
所以
,
运行时常量池中的内容包含
:
Class常量池中的常量
、
字符串常量池中的内容
###
运行时常量池
、
Class常量池
、
字符串常量池的区别与联系
虚拟机启动过程中
,
会将各个Class文件中的常量池载入到运行时常量池中
。
所以
,
Class常量池只是一个媒介场所
。
在JVM真的运行时
,
需要把常量池中的常量加载到内存中
,
进入到运行时常量池
。
字符串常量池可以理解为运行时常量池分出来的部分
。
加载时
,
对于class的静态常量池
,
如果字符串会被装到字符串常量池中
。
You can’t perform that action at this time.