java中jol-core依赖分析对象内存模型
1、引用jol-core依赖
jol-core依赖版本的不同,下面代码分析的结果样式是不同的,下面再展示不同版本的结果
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.16</version>
<!-- <scope>provided</scope>-->
</dependency>
2、代码分析
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;
import org.openjdk.jol.vm.VM;
public class test {
public static void main(String[] args) {
String s = "-1111111111111111111111111011011011110110100001001010001011110111";
// String s = "- 11111111【】 11111111【】 11111111【】 10110110【b6】 11110110【f6】 10000100【84】 10100010【a2】 11110111【f7】";
String b1 = "11110111";
System.out.println("s的长度是="+s.length());
System.out.println("=============================");
Person person = new Person();
// int hashCode = person.hashCode();
System.out.println(VM.current().details());
System.out.println("============================");
// String toHexString = Integer.toHexString(person.hashCode()); 136432db
System.out.println("10进制》》System.identityHashCode(person)="+System.identityHashCode(person));
System.out.println("16进制》》System.identityHashCode(person)="+Integer.toHexString(System.identityHashCode(person)));
System.out.println("hashcode转成16进制的string=" +Integer.toHexString(person.hashCode()));
System.out.println("=============================================");
System.out.println(GraphLayout.parseInstance(person).totalSize());
System.out.println("手动触发一次gc");
System.gc();
System.out.println("===========================================");
print(person);
}
static void print(Person p){
System.out.println("-----------------------------------------");
System.err.println(ClassLayout.parseInstance(p).toPrintable());
}
private static class Person {
private Integer age;
private String name;
private String address;
}
}
3、当version是0.16时,结果如下
s的长度是=65
=============================
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-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]
============================
10进制》》System.identityHashCode(person)=325333723
16进制》》System.identityHashCode(person)=136432db
hashcode转成16进制的string=136432db
=============================================
24
手动触发一次gc
===========================================
-----------------------------------------
com.xxx.test$Person object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x000000136432db09 (hash: 0x136432db; age: 1)
8 4 (object header: class) 0xf800c143
12 4 java.lang.Integer Person.age null
16 4 java.lang.String Person.name null
20 4 java.lang.String Person.address null
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
Process finished with exit code 0
4、当version是0.09时,结果如下
s的长度是=65
=============================
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-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]
============================
10进制》》System.identityHashCode(person)=325333723
16进制》》System.identityHashCode(person)=136432db
hashcode转成16进制的string=136432db
=============================================
24
手动触发一次gc
===========================================
-----------------------------------------
com.xxx.test$Person object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 09 db 32 64 (00001001 11011011 00110010 01100100) (1681054473)
4 4 (object header) 13 00 00 00 (00010011 00000000 00000000 00000000) (19)
8 4 (object header) 43 c1 00 f8 (01000011 11000001 00000000 11111000) (-134168253)
12 4 java.lang.Integer Person.age null
16 4 java.lang.String Person.name null
20 4 java.lang.String Person.address null
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
Process finished with exit code 0