内存中的Java原始数组布局

时间:2023-01-01 17:20:32

Here are the two samples I would like to base my question on (assuming you have JOL here):

以下是我想提出问题的两个样本(假设你在这里有JOL):

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

And an example using this:

并使用此示例:

int [] ints = new int[10];  

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

And here are the two outputs:

这是两个输出:

  [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

I mainly understand the output, the thing I don't is what are these:

我主要理解输出,我不知道的是这些:

12 bytes external and 8 bytes external

In general, Objects are 8 bytes aligned, so why the need to add more padding than needed?

通常,对象是8字节对齐的,那么为什么需要添加比所需更多的填充?

I know about a few things that are somehow weird, the first one has to do with the API that JOL is using and the second has to do with internal data, that needs to be hidden.

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

I also know about this, but it seems un-related, as it means internal padding.

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

Can someone shed some light on this?

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

1 个解决方案

#1


4  

Instance size: 64 bytes is calculated for the current VM configuration, but you explicitly specify different (incompatible) Layouter.

实例大小:为当前VM配置计算64个字节,但您明确指定了不同(不兼容)的Layouter。

The difference between actual size (calculated with Instrumentation.getObjectSize) and the expected size (calculated by Layouter) will be treated as loss due to the next object alignment.

实际大小(使用Instrumentation.getObjectSize计算)与预期大小(由Layouter计算)之间的差异将被视为由于下一个对象对齐而导致的丢失。

See ClassLayout.java

请参见ClassLayout.java

#1


4  

Instance size: 64 bytes is calculated for the current VM configuration, but you explicitly specify different (incompatible) Layouter.

实例大小:为当前VM配置计算64个字节,但您明确指定了不同(不兼容)的Layouter。

The difference between actual size (calculated with Instrumentation.getObjectSize) and the expected size (calculated by Layouter) will be treated as loss due to the next object alignment.

实际大小(使用Instrumentation.getObjectSize计算)与预期大小(由Layouter计算)之间的差异将被视为由于下一个对象对齐而导致的丢失。

See ClassLayout.java

请参见ClassLayout.java