JOL的全稱是Java Object Layout抽高。是一個用來分析JVM中Object布局的小工具道盏。包括Object在內(nèi)存中的占用情況因苹,實例對象的引用情況等等鞭呕。
引入依賴:
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.10</version>
</dependency>
相關(guān)API方法:
private static void testObj() {
Object obj = new Object();
System.out.println(ClassLayout.parseInstance(obj).toPrintable());
}
1.object對象占用幾個字節(jié)
答案:16字節(jié)蛤育。
其中對象頭占用12字節(jié),對齊填充4的字節(jié)葫松。
2.String對象占用幾個字節(jié)
答案JDK11版本中缨伊,占用24字節(jié)。
其中對象頭占用12字節(jié)进宝,byte[]數(shù)組占用4字節(jié)刻坊,int類型的hash屬性占用4字節(jié),byte類型的coder占用1字節(jié)党晋,對齊填充3個字節(jié)谭胚。
java.lang.String object internals:
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) 08 18 00 00 (00001000 00011000 00000000 00000000) (6152)
12 4 byte[] String.value [97, 97, 97]
16 4 int String.hash 0
20 1 byte String.coder 0
21 3 (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
3.Integer類型占用幾個字節(jié)(基礎(chǔ)類型的裝箱)
答案:占用16字節(jié)。
其中對象頭12字節(jié)未玻,實例數(shù)據(jù)int占用4字節(jié)灾而;
java.lang.Integer object internals:
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) e8 39 01 00 (11101000 00111001 00000001 00000000) (80360)
12 4 int Integer.value 100000
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
4.Long占用幾個字節(jié)
答案:占用24個字節(jié)。
其中對象頭12字節(jié)扳剿,實例數(shù)據(jù)long占用8字節(jié)旁趟,對齊填充4字節(jié)(必須是8的整數(shù)倍)。
java.lang.Long object internals:
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) 38 3c 01 00 (00111000 00111100 00000001 00000000) (80952)
12 4 (alignment/padding gap)
16 8 long Long.value 100000
Instance size: 24 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total
5.Object[100]占用幾個字節(jié)
答案:416字節(jié)庇绽。
其中對象頭16字節(jié)锡搜,對齊填充100*4=400字節(jié)。
[Ljava.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) 88 3e 01 00 (10001000 00111110 00000001 00000000) (81544)
12 4 (object header) 64 00 00 00 (01100100 00000000 00000000 00000000) (100)
16 400 java.lang.Object Object;.<elements> N/A
Instance size: 416 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
5.String[100]占用幾個字節(jié)
答案:416字節(jié)瞧掺。
其中對象頭16字節(jié)耕餐,String的實例數(shù)據(jù)+對齊填充占用400字節(jié)。
[Ljava.lang.String; 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) 58 55 02 00 (01011000 01010101 00000010 00000000) (152920)
12 4 (object header) 64 00 00 00 (01100100 00000000 00000000 00000000) (100)
16 400 java.lang.String String;.<elements> N/A
Instance size: 416 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total