9

这是我想提出问题的两个示例(假设您在JOL这里):

Layouter layout32Bits =  new HotSpotLayouter(new X86_32_DataModel());
Layouter layout64BitsComp = new HotSpotLayouter(new X86_64_COOPS_DataModel());

还有一个使用这个的例子:

int [] ints = new int[10];  

System.out.println(ClassLayout.parseInstance(ints, layout32Bits).toPrintable());
System.out.println(ClassLayout.parseInstance(ints, layout64BitsComp).toPrintable());

这是两个输出:

  [I object internals:
  OFFSET  SIZE   TYPE DESCRIPTION             VALUE
  0     4        (object header)     09 00 00 00 (00001001 00000000 00000000 00000000) (9)
  4     4        (object header)     00 00 00 00 (00000000 00000000 00000000 00000000) (0)
  8     4        (object header)     10 1b 0c 1a (00010000 00011011 00001100 00011010) (437000976)
 12    40    int [I.<elements>            N/A
 52    12        (loss due to the next object alignment)
 Instance size: 64 bytes
 Space losses: 0 bytes internal + 12 bytes external = 12 bytes total

  [I object internals:
  OFFSET  SIZE   TYPE DESCRIPTION             VALUE
  0     4        (object header)     09 00 00 00 (00001001 00000000 00000000 00000000) (9)
  4     4        (object header)     00 00 00 00 (00000000 00000000 00000000 00000000) (0)
  8     4        (object header)     10 1b 0c 1a (00010000 00011011 00001100 00011010) (437000976)
 12     4        (object header)     01 00 00 00 (00000001 00000000 00000000 00000000) (1)
 16    40    int [I.<elements>            N/A
 56     8        (loss due to the next object alignment)
 Instance size: 64 bytes
 Space losses: 0 bytes internal + 8 bytes external = 8 bytes total

我主要了解输出,我不了解的是这些:

12 bytes external and 8 bytes external

一般来说,对象是8 bytes对齐的,那么为什么需要添加比需要更多的填充?

我知道一些奇怪的事情,第一个与 JOL 正在使用的 API 有关,第二个与需要隐藏的内部数据有关

我也知道这一点,但它似乎无关,因为它意味着内部填充

有人可以对此有所了解吗?

4

1 回答 1

6

Instance size: 64 bytes是针对当前 VM 配置计算的,但您明确指定了不同的 (incompatible) Layouter

实际尺寸(用 计算Instrumentation.getObjectSize)与预期尺寸(用 Layouter 计算)之间的差异将被视为loss due to the next object alignment

请参见ClassLayout.java

于 2018-05-24T13:00:45.677 回答