UseCompressedOops 在 jdk-13 和 jdk-15 中使用 CompressedClassPointers

UseCompressedOops UseCompressedClassPointers in jdk-13 and jdk-15

我无意中发现了 jdk-15 中的一个我不知道的变化。假设我有一个非常简单的问题:3 个整数数组的大小是多少?为此,我使用 JOL。代码相当简单:

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;

public class Array {
    public static void main(String [] args){
       int [] array = new int[3];
       System.out.println(ClassLayout.parseInstance(array).toPrintable());
    }
}

我运行这个用jdk-13:

  java -Djdk.attach.allowAttachSelf -Djol.tryWithSudo=true -cp jol-cli.jar  Array.java

我得到输出:

    [I object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           18 0e 07 00 (00011000 00001110 00000111 00000000) (462360)
     12     4        (object header)                           03 00 00 00 (00000011 00000000 00000000 00000000) (3)
     16    12    int [I.<elements>                             N/A
     28     4        (loss due to the next object alignment)
Instance size: 32 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

这很明显:

     12 bytes --> Object headers
     4  bytes --> size of array
     12 bytes --> elements of array themselves
     4  bytes --> padding to align by 8 bytes
     ----
     32 bytes total

运行 这个带有 jdk-15 的例子产生相同的输出,相同的 32 bytes。预计...


对于第二部分,我想禁用 JVM 优化:-XX:-UseCompressedOops。我 运行 这个 jdk-13:

java -Djdk.attach.allowAttachSelf -Djol.tryWithSudo=true -cp jol-cli.jar -XX:-UseCompressedOops  Array.java


    [I object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           11 00 00 00 (00010001 00000000 00000000 00000000) (17)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           40 0c f0 33 (01000000 00001100 11110000 00110011) (871369792)
     12     4        (object header)                           02 00 00 00 (00000010 00000000 00000000 00000000) (2)
     16     4        (object header)                           03 00 00 00 (00000011 00000000 00000000 00000000) (3)
     20     4        (alignment/padding gap)
     24    12    int [I.<elements>                             N/A
     36     4        (loss due to the next object alignment)
Instance size: 40 bytes
Space losses: 4 bytes internal + 4 bytes external = 8 bytes total

好吧,有点意料之中:

     16 bytes --> object headers (I did -XX:-UseCompressedOops after all)
     4 bytes  --> array size
     4 bytes  --> alignment for array headers (AFAIK this is only done for arrays)
     12 bytes --> array elements themselves
     4 bytes  --> 4 bytes padding
     ----
     40 bytes total

现在让我们运行与jdk-15相同的例子:

[I object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           0e 09 00 00 (00001110 00001001 00000000 00000000) (2318)
     12     4        (object header)                           03 00 00 00 (00000011 00000000 00000000 00000000) (3)
     16    12    int [I.<elements>                             N/A
     28     4        (loss due to the next object alignment)
Instance size: 32 bytes

为什么现在是 32 bytes?为什么不是 40,就像 jdk-13?

jdk-13jdk-15 中,这两个选项都默认启用:

java -XX:+PrintFlagsFinal -version | grep Compressed

 bool UseCompressedClassPointers  = true                                 
 bool UseCompressedOops           = true

-XX:-UseCompressedOops被禁用时,意味着UseCompressedClassPointers也被禁用。这就是为什么当 UseCompressedOops 关闭时,header 大小会增加 4 bytes,因为 UseCompressedOops 会关闭 UseCompressedClassPointers。至少在 jdk-13:

中是这样
  java -XX:+PrintFlagsFinal -XX:-UseCompressedOops -version | grep Compressed

    bool UseCompressedClassPointers = false                                  
    bool UseCompressedOops          = false

jdk-15 发生了变化:

    bool UseCompressedClassPointers = true                                 
    bool UseCompressedOops          = false

所以禁用 UseCompressedOops 并不意味着 UseCompressedClassPointers 也被禁用,所以它停留在 4 bytes.

不过,我自己回答了这个问题,如果有人能找到相关的 bug/change 就好了?到目前为止,我还没有成功。