繁体   English   中英

在 jdk 1.8+ 中,元空间如何布局 class 信息?

[英]In jdk 1.8+, how metaspace layout the class info?

在jdk1.8+中,MetaspaceTest class加载到jvm时,这是我对memory布局的理解,对吗?

public class MetaspaceTest {
    /* 
     1. "a" would be interned into the global String Table of heap;
     2. object created by [new String("a")] would be placed in heap;
     3. constant STR_OBJ would be placed in the constant-pool of MetaspaceTest class info, 
        and pointing to the string object created by [new String("a")];
    */
    private final static String STR_OBJ = new String("a");
    /* 
     4. "b" would be interned in the global String Table of heap;
     5. static field literalStr would be placed in metaspace, 
       and pointing to the string "b" which is interned into the global String Table of heap;
    */
    private static String literalStr = "b";
    /*
     6. object created by [new Integer(8)] would be placed in heap;
     7. static field intNum would be placed in metaspace, 
       and pointing to the object created by [new Integer(8)];
    */
    private static Integer intNum = new Integer(8);
}

首先,像Metaspace、String Table之类的东西,在JLS或者JVMS中都没有规定。 它完全是特定 JVM 的内部实现细节。例如,完全兼容的 JVM 实现根本没有元空间。

如果我们谈论现代 HotSpot JVM,您的理解不太正确。

  1. 不。代表"a"java.lang.String object 的一个实例在 Java 堆中。 只要"a"是强可达的,字符串表就包含对 object 的引用。
  2. 是的。
  3. 不。 运行时常量池包含JVMS §4.4中描述的内容。 Static字段属于一个class镜像,它是Java Heap中java.lang.Class的一个实例。
  4. 与1相同。
  5. 元空间(如“元”前缀所示)包含class 元数据,但不包含数据本身。 例如,元空间包含以下信息:class MetaspaceTest有一个名为literalStr的字段,类型为java.lang.String ,但它与该字段的值无关。
  6. 是的。
  7. 没有。如上所述,元空间只包含字段的描述,不包含数据。 实例字段的实际持有者是一个object实例; static字段的实际持有者是class镜像,即java.lang.Class在堆中的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM