查看java一个对象的字节大小

查看对象占用字节数

<!-- pom引入jar包 -->
<dependency>
    <groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.10</version>
</dependency>
//查看object对象字节
public static void main(String[] args) {
            Object o = new Object();
            String s = ClassLayout.parseInstance(o).toPrintable();
            System.out.println(s);
        }
//输出结果
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)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

前两行是默认的 前MarkWord 固定8个字节 ,用来存储 锁信息,和记录GC中对象被回收了多少次

第三行是存储的对象指针,4字节

最后是一位是padding补位,64位计算机存储需要是8的倍数

你可能感兴趣的:(Java,java,开发语言,后端)