引入jar包
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.9</version>
</dependency>
使用案例
package com.tiger;
import org.openjdk.jol.info.ClassLayout;
/**
* @description:
* @author: tiger
* @create: 2021-05-29 21:46
*/
public class CLassLayoutDemo {
public static void main(String[] args) {
CLassLayoutDemo demo = new CLassLayoutDemo();
System.out.println(ClassLayout.parseInstance(demo).toPrintable());
}
}
输出结果
com.tiger.CLassLayoutDemo 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)
// Class Poionter
8 4 (object header) 05 c1 00 20 (00000101 11000001 00000000 00100000) (536920325)
// 对其填充
12 4 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4