添加依赖
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.17</version>
</dependency>
查看对象头大小
@Test
void header() {
ClassLayout layout = ClassLayout.parseInstance(new Object());
System.out.println(layout.toPrintable());
ClassLayout layout2 = ClassLayout.parseInstance(new int[]{});
System.out.println(layout2.toPrintable());
User user = new User();
user.setAge(2);
user.setName("hello");
ClassLayout layout3 = ClassLayout.parseInstance(user);
System.out.println(layout3.toPrintable());
}
@Data
public static class User {
private String name;
private Integer age;
private String remark;
}
输出:
JavaObjectHeaderTest$User object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000001 (non-biasable; age: 0)
8 4 (object header: class) 0x00d00eb0
12 4 java.lang.String User.name (object)
16 4 java.lang.Integer User.age 2
20 4 java.lang.String User.remark null
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
创建一个自定义User对象,对象头大小为24 Bytes
标签:ClassLayout,Java,String,查看,对象,object,bytes,User,user From: https://www.cnblogs.com/jiayuan2006/p/18375446