元空间和运行时常量池 - MaXianZhe

博客园 · · 187 次点击 · · 开始浏览    

元空间是方法区的实现

方法区是什么呢?

  从java虚拟机规范9里摘抄的

It stores per-class structures such as the run-time
constant pool, field and method data, and the code for methods and constructors,
including the special methods used in class and interface initialization and in
instance initialization 

包括了如下的几个部分

  1 运行时常量池

  2 字段和方法数据

  3 方法和构造方法的字节码

而关于运行时常量池

A run-time constant pool is a per-class or per-interface run-time representation
of the constant_pool table in a class file (§4.4). It contains several kinds of
constants, ranging from numeric literals known at compile-time to method and field
references that must be resolved at run-time. The run-time constant pool serves a
function similar to that of a symbol table for a conventional programming language,
although it contains a wider range of data than a typical symbol table.
Each run-time constant pool is allocated from the Java Virtual Machine's method
area (§2.5.4). The run-time constant pool for a class or interface is constructed
when the class or interface is created (§5.3) by the Java Virtual Machine.
The following exceptional condition is associated with the construction of the runtime constant pool for a class or interface:
• When creating a class or interface, if the construction of the run-time constant
pool requires more memory than can be made available in the method area of the
Java Virtual Machine, the Java Virtual Machine throws an OutOfMemoryError.
See §5 (Loading, Linking, and Initializing) for information about the construction of the
run-time constant pool.

翻译过来就是,运行时常量池是class文件里常量池的运行时代表(run-time representation) 实在不知道怎么翻译好,各位理解就行了。

numeric literals数字字面量。

method and field references that must be resolved at run-time 方法和字段的引用只有在运行时才能解析,很好理解不到jvm里没法知道代码所在地址

为什么要在直接内存里拿出来一块内存作为元空间取代永久代呢?主要的说法有以下几个:

(1)类及方法的信息等比较难确定其大小,因此对于永久代的大小指定比较困难,太小容易出现永久代溢出,太大则容易导致老年代溢出。

(2)永久代会为 GC 带来不必要的复杂度,并且回收效率偏低。

常量池随永久代的变化

几种常量池:

(1)静态常量池:即*.class文件中的常量池,在Class文件结构中,最头的4个字节存储魔数,用于确定一个文件是否能被JVM接受,接着4个字节用于存储版本号,前2个为次版本号,后2个主版本号,再接着是用于存放常量的常量池,由于常量的数量是不固定的,所以常量池的入口放置一个U2类型的数据(constant_pool_count)存储常量池容量计数值。

这种常量池占用class文件绝大部分空间,主要用于存放两大类常量:字面量和符号引用量,字面量相当于Java语言层面常量的概念,如文本字符串、基础数据、声明为final的常值等;符号引用则属于编译原理方面的概念,包括了如下三种类型的常量:类和接口的全限定名、字段名称描述符、方法名称描述符。类的加载过程中的链接部分的解析步骤就是把符号引用替换为直接引用,即把那些描述符(名字)替换为能直接定位到字段、方法的引用或句柄(地址)。

(2)运行时常量池:虚拟机会将各个class文件中的常量池载入到运行时常量池中,即编译期间生成的字面量、符号引用,总之就是装载class文件。为什么它叫运行时常量池呢?因为这个常量池在运行时,里面的常量是可以增加的。如:“+”连接字符生成新字符后调用 intern()方法、生成基础数据的包装类型等等。

(3)字符串常量池 :字符串常量池可以理解为是分担了部分运行时常量池的工作。加载时,对于class文件的静态常量池,如果是字符串就会被装到字符串常量池中。

(4)整型常量池:Integer,类似字符串常量池。管理-128--127的常量。类似的还有Character、Long等常量池(基本数据类型没有,Double、Float也没有常量池)

总结就是:

class文件有常量池存放这个类的信息,占用了大多数空间。但是运行时所有加载进来的class文件的常量池的东西都要放到运行时常量池,这个运行时常量池还可以在运行时添加常量。字符串常量池、Integer等常量池则是分担了运行时常量池的工作,

在永久代移除后,字符串常量池也不再放在永久代了,但是也没有放到新的方法区---元空间里,而是留在了堆里(为了方便回收?)。运行时常量池当然是随着搬家到了元空间里,毕竟它是装类的重要信息的,有它的地方才称得上是方法区。

  参考了 https://www.cnblogs.com/shen-qian/p/11277085.html

 

本文来自:博客园

感谢作者:博客园

查看原文:元空间和运行时常量池 - MaXianZhe

187 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传