Hexo个人博客
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4.4 KiB

title tags categories abbrlink date keywords description cover
使用jolcore库查看Java对象大小 Java技术 技术随笔 155251202 2024-03-011 14:22:05 <nil> <nil> <nil>

对象大小可以用jolcore包查看,本篇介绍jolcore的基本使用

引入依赖

<dependency>
    <groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.9</version>
</dependency>

具体使用

ClassLayout layout = ClassLayout.parseInstance(new Object());
System.out.println(layout.toPrintable());

测试代码

import org.openjdk.jol.info.ClassLayout;
public class JOLSample {
    public static void main(String[] args) {
        ClassLayout layout = ClassLayout.parseInstance(new Object());
        System.out.println(layout.toPrintable());
        System.out.println();
        ClassLayout layout1 = ClassLayout.parseInstance(new int[]{1,2,3,4,5});
        System.out.println(layout1.toPrintable());
        System.out.println();
        ClassLayout layout2 = ClassLayout.parseInstance(new A());
        System.out.println(layout2.toPrintable());
    }
    // -XX:+UseCompressedOops           默认开启的压缩所有指针
    // -XX:+UseCompressedClassPointers  默认开启的只压缩对象头里的类型指针Klass Pointer
    // Oops:Ordinary Object Pointers
    //堆内存大于32G时,指针压缩会失效。
    //堆内存小于4G时,不需要启用指针压缩,jvm会直接去除高32位地址。
    public static class A {
        //8B mark word
        //4B Klass Pointer
        int id = 6;             //4B
        String name;        //4B
        byte b;             //1B
        Object o;           //4B
    }
}

打印结果

java.lang.Object 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)                           00 1c 2f c5 (00000000 00011100 00101111 11000101) (-986768384)
     12     4        (object header)                           9a 02 00 00 (10011010 00000010 00000000 00000000) (666)
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

[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)                           68 0b 2f c5 (01101000 00001011 00101111 11000101) (-986772632)
     12     4        (object header)                           9a 02 00 00 (10011010 00000010 00000000 00000000) (666)
     16     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
     20     4        (alignment/padding gap)                  
     24    20    int [I.<elements>                             N/A
     44     4        (loss due to the next object alignment)
Instance size: 48 bytes
Space losses: 4 bytes internal + 4 bytes external = 8 bytes total

com.locaris.jvm.JOLSample$A 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)                           10 f6 9c c5 (00010000 11110110 10011100 11000101) (-979569136)
     12     4                    (object header)                           9a 02 00 00 (10011010 00000010 00000000 00000000) (666)
     16     4                int A.id                                      6
     20     1               byte A.b                                       0
     21     3                    (alignment/padding gap)                  
     24     8   java.lang.String A.name                                    null
     32     8   java.lang.Object A.o                                       null
Instance size: 40 bytes
Space losses: 3 bytes internal + 0 bytes external = 3 bytes total