JOL = Java Object Layout 分析对象在JVM的大小和分布
官网:http://openjdk.java.net/projects/code-tools/jol/
pom依赖
org.openjdk.jol
jol-core
0.9
上代码:
public classTest {staticObject getObject() {
Map map = new HashMap<>();
map.put("a", 1L);
map.put("b", "b");
map.put("c", newDate());for (int i = 0; i < 10; i++) {
map.put(String.valueOf(i), String.valueOf(i));
}returnmap;
}public static voidmain(String[] args) {
Object obj=getObject();//查看对象内部信息
System.out.println(ClassLayout.parseInstance(obj).toPrintable());
System.out.println("----------我是分割线---------------");//查看对象外部信息
System.out.println(GraphLayout.parseInstance(obj).toPrintable());
System.out.println("----------我是分割线---------------");//获取对象总大小
System.out.println("size : " +GraphLayout.parseInstance(obj).totalSize());
}
}