3

如何得到一个对象真实的内存大小

介绍一款工具(memory-measurer)可方便的测量一个对象真实占用内存大小 如有这么一个User对象

public class User {
    private Integer id;
    private String mobile;
    private Date createTime;
}   

先看一个空User对象的内存占用量

User u = new User();
System.out.println(MemoryMeasurer.measureBytes(u)); //24
System.out.println(ObjectGraphMeasurer.measure(u)); //Footprint{Objects=1, References=3, Primitives=[]}

可知一个对象 三个引用 共占了24字节

逐个赋值后占用内存是多少呢?

// 给id赋值
Integer id = new Integer(1);
System.out.println(MemoryMeasurer.measureBytes(id)); // 16

u.setId(id);
System.out.println(MemoryMeasurer.measureBytes(u)); // 40
System.out.println(ObjectGraphMeasurer.measure(u)); //Footprint{Objects=2, References=3, Primitives=[int]}

一个Integer对象占用16字节 于是给id赋值后 user对象变成了24+16=40字节了。

// 给mobile赋值
String mobile = "13600000001";    
System.out.println(MemoryMeasurer.measureBytes(mobile)); // 64
u.setMobile(mobile);
System.out.println(MemoryMeasurer.measureBytes(u)); // 104
System.out.println(ObjectGraphMeasurer.measure(u)); //Footprint{Objects=4, References=4, Primitives=[int x 2, char x 11]}

一个11位长的mobile字符串对象占用了64字节,于是user对象变成了40+64=104字节

// 给createTime赋值
Date createTime = new Date();
System.out.println(MemoryMeasurer.measureBytes(createTime)); // 24字节
u.setCreateTime(createTime);
System.out.println(MemoryMeasurer.measureBytes(u)); // 128
System.out.println(ObjectGraphMeasurer.measure(u)); //Footprint{Objects=5, References=5, Primitives=[int x 2, long, char x 11]}

可知一个Date对象占用了24字节, 于是全部属性不为空的一个User对象占用内存为128字节。

另外还可以通过另外一个工具--JOL (Java Object Layout)--可知更详细的Footprint信息

通过上面的工具我们只是知道一个空User对象占用了24字节以及简单的

Footprint{Objects=1, References=3, Primitives=[]}

通过此工具可知这24个字节是怎么分配的了

System.out.println(ClassLayout.parseClass(User.class).toPrintable());

memorymeasurer.User object internals:
 OFFSET  SIZE    TYPE DESCRIPTION                    VALUE
      0    12         (object header)                N/A
     12     4 Integer User.id                        N/A
     16     4  String User.mobile                    N/A
     20     4    Date User.createTime                N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

上面我们知道一个Integer对象占用了16字节 看这16个字节是怎么分配的

System.out.println(ClassLayout.parseClass(Integer.class).toPrintable());

java.lang.Integer object internals:
 OFFSET  SIZE  TYPE DESCRIPTION                    VALUE
      0    12       (object header)                N/A
     12     4   int Integer.value                  N/A
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

上面我们知道一个11位长的String对象占用了64字节 看其是怎么分配的

System.out.println(ClassLayout.parseClass(String.class).toPrintable());

java.lang.String object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                    VALUE
      0    12        (object header)                N/A
     12     4 char[] String.value                   N/A
     16     4    int String.hash                    N/A
     20     4        (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

即一个空String对象占用了24字节

System.out.println(ClassLayout.parseClass(char[].class).toPrintable());

[C object internals:
 OFFSET  SIZE  TYPE DESCRIPTION                    VALUE
      0    16       (object header)                N/A
     16     0  char [C.<elements>                  N/A
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

一个长度为0的char数组占了16字节 于是11位长的char数组占用字节为: 16+2*11=38 因为需要按8字节对齐 于是还得加上2字节的填充符 于是变成了40字节。 所以一个11位长的字符串的占用字节为24+40=64

补充

memory-measurer如何使用

git clone https://github.com/msteindorfer/memory-measurer
cd memory-measurer
mvn clean install

pom文件中添加依赖

        <dependency>
            <groupId>com.github.msteindorfer</groupId>
            <artifactId>memory-measurer</artifactId>
            <version>0.1.0-SNAPSHOT</version>
        </dependency>

运行时时显式添加vm参数 如

-javaagent:/Users/zhugw/workspace/memory-measurer/target/memory-measurer-0.1.0-SNAPSHOT.jar

jol使用说明

只需添加依赖

        <dependency>
            <groupId>org.openjdk.jol</groupId>
            <artifactId>jol-core</artifactId>
            <version>0.9</version>
        </dependency>

相关参考文档

https://github.com/msteindorf...
http://openjdk.java.net/proje...
http://blog.omalley.id.au/201...


zhuguowei2
825 声望26 粉丝