如何獲得一個對象真實的內存大小

如何獲得一個對象真實的內存大小

介紹一款工具(memory-measurer)可方便的測量一個對象真實佔用內存大小 若有這麼一個User對象html

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

先看一個空User對象的內存佔用量java

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

可知一個對象 三個引用 共佔了24字節git

逐個賦值後佔用內存是多少呢?github

// 給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字節。spa

另外還能夠經過另一個工具--JOL (Java Object Layout)--可知更詳細的Footprint信息.net

經過上面的工具咱們只是知道一個空User對象佔用了24字節以及簡單的code

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

經過此工具可知這24個字節是怎麼分配的了xml

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...

相關文章
相關標籤/搜索