【问题标题】:Why compressed Oops gives 12 bytes for Object Header为什么压缩的 Oops 为对象标头提供 12 个字节
【发布时间】:2014-10-09 13:14:39
【问题描述】:

这是在 Java 6 内存模型之后。在 32 位 JVM 中,对象的 Shallow 大小为

8 bytes (object header) + total of all instance variables + padding (optional)

如果前 2 个项加起来不是 8 的倍数,则会有填充。

在 64 位 JVM 中,Shallow 大小为

16 bytes (object header) + total of all instance variables + padding (optional)

我的理解是这个Object header由2个词组成(oracle热点VM)

  • 一个经典词
  • 一个标记词

在 32 位 JVM 上,对象标头 = 2 * 32 位 = 64 位 = 8 个字节
在 64 位 JVM 上,对象头 = 2 * 64 位 = 128 位 = 16 字节

但是使用 CompressedOops,3 个低位被截断,因此对于小于 32 gigs 的堆,在 64 位 JVM 上它应该回到 8 个字节

但是当我使用 JOL(Java 对象布局)测试对象布局时,它显示了 12 个字节的对象头。

测试代码

public class App  {
    public static void main( String[] args )
    {
        System.out.println(System.getProperty("java.version"));
        System.out.println(VMSupport.vmDetails());
        System.out.println(ClassLayout.parseClass(A.class).toPrintable());
    } 
}

class A {
   int a; 
}

输出

1.8.0_05
Running 64-bit HotSpot VM.
Using compressed references with 3-bit shift.
Objects are 8 bytes aligned.
Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

com.layout.test.jolTesting.A object internals:
 OFFSET  SIZE  TYPE DESCRIPTION                    VALUE
      0    12       (object header)                N/A
     12     4   int A.a                            N/A
Instance size: 16 bytes (estimated, the sample instance is not available)
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

我在这里缺少什么会增加额外的 4 个字节?

【问题讨论】:

  • 你在使用 HotSpot 吗?
  • 是 Oracle 热点 JDK 8 虚拟机。我现在在我的问题中添加了输出和代码

标签: java memory-layout objectsize


【解决方案1】:

据我所知,这是因为与 klass 词相反,标记词不是使用 CompressedOops 编码的。

所以4字节(64位压缩klass字)+8字节(标记字)=12字节(头)

【讨论】:

  • 谢谢.. 现在说得通了 :)
  • @andresp 这是完全正确的,CompressedOops 影响klass 和普通引用。
【解决方案2】:

HotSpot 具有 8 字节、12 字节和 16 字节的对象标头。这是因为标头包含两部分,markword(关于对象的元信息)和 classword(对类的引用)。在 32/64 位模式中,标记字占用 4 或 8 个字节。 Classword 只是“引用”,因此可以在 64 位模式下压缩。

见:https://shipilev.net/blog/2014/heapdump-is-a-lie/

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2023-01-26
    • 1970-01-01
    • 2013-09-18
    • 2015-02-11
    • 2015-03-28
    • 1970-01-01
    • 2016-04-22
    • 2016-03-15
    相关资源
    最近更新 更多