作者:LXY520TB_194 | 来源:互联网 | 2022-12-21 14:31
我使用了一个名为JOL(Java Object Layout)的工具,它试图分析对象布局.它带有一个cli
,我用它来分析java.lang.Integer
.我看到Integer对象占用了12个额外的字节用于开销.该开销可以是4个字节用于该对象所属的类的地址,另外4个用于垃圾收集,但剩余的4个字节呢?我知道对象有一个整数hashCode值,但我不认为它是唯一的(即它不使用内存的位置,它使用原始值代替)因为:
Integer a = new Integer(12);
Integer b = new Integer(12);
System.out.println(a.hashCode() == 12 && b.hashCode() == 12);
// prints: true
日志:
$ java -jar jol-cli/target/jol-cli.jar internals java.lang.Integer
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# Running 64-bit HotSpot VM.
# Using compressed oop with 0-bit shift.
# Using compressed klass with 0-bit shift.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Instantiated the sample instance via public java.lang.Integer(int)
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 88 26 cf 22 (10001000 00100110 11001111 00100010) (584001160)
12 4 int Integer.value 0
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
Stephen C..
6
......但剩下的4个字节呢?
丢失的4个字节将填充到下一个8字节边界.Java堆节点的大小为8字节的倍数.
但由于这是一个64位JVM,标记字是8字节而不是4字节,总共产生12字节的标头开销.
1> Stephen C..:
......但剩下的4个字节呢?
丢失的4个字节将填充到下一个8字节边界.Java堆节点的大小为8字节的倍数.
但由于这是一个64位JVM,标记字是8字节而不是4字节,总共产生12字节的标头开销.