https://github.com/openjdk/jol
<!--查看对象的大小-->
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.16</version>
</dependency>
MemoryEntity memoryEntity = new MemoryEntity();
//输出对象内部信息
System.out.println("对象内部大小==>" + ClassLayout.parseInstance(memoryEntity).toPrintable());
//输出对象外部信息,包含引用对象
System.out.println(GraphLayout.parseInstance(memoryEntity).toPrintable());
//输出对象大小
System.out.println(GraphLayout.parseInstance(memoryEntity).totalSize());
/*
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
对象内部大小==>com.zs.博客.分析对象大小.MemoryEntity object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000001 (non-biasable; age: 0)
8 4 (object header: class) 0xf800c143
12 4 int MemoryEntity.intField 0
16 8 long MemoryEntity.longField 0
24 2 char MemoryEntity.charField
26 1 boolean MemoryEntity.booleanField false
27 1 (alignment/padding gap)
28 4 java.lang.String MemoryEntity.stringField null
32 4 java.lang.String MemoryEntity.StringField2 (object)
36 4 java.util.concurrent.atomic.AtomicInteger MemoryEntity.atomicInteger null
Instance size: 40 bytes
Space losses: 1 bytes internal + 0 bytes external = 1 bytes total
com.zs.博客.分析对象大小.MemoryEntity@21a06946d object externals:
ADDRESS SIZE TYPE PATH VALUE
76ad5ad30 40 com.zs.博客.分析对象大小.MemoryEntity (object)
76ad5ad58 24 java.lang.String .StringField2 (object)
76ad5ad70 32 [C .StringField2.value [z, s, , j, a, v, a]
Addresses are stable after 1 tries.
96
96字节 / 1024 = 0.09375kb
*/