如何得到一个对象真实的内存大小
BrainRidgew
8年前
<h3><strong>如何得到一个对象真实的内存大小</strong></h3> <p>介绍 一款工具 ( memory-measurer )可方便的测量一个对象真实占用内存大小 如有这么一个User对象</p> <pre> <code class="language-java">public class User { private Integer id; private String mobile; private Date createTime; }</code></pre> <p>先看一个空User对象的内存占用量</p> <pre> <code class="language-java">User u = new User(); System.out.println(MemoryMeasurer.measureBytes(u)); //24 System.out.println(ObjectGraphMeasurer.measure(u)); //Footprint{Objects=1, References=3, Primitives=[]}</code></pre> <p>可知一个对象 三个引用 共占了24字节</p> <p>逐个赋值后占用内存是多少呢?</p> <pre> <code class="language-java">// 给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]}</code></pre> <p>一个Integer对象占用16字节 于是给id赋值后 user对象变成了 24+16=40 字节了。</p> <pre> <code class="language-java">// 给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]}</code></pre> <p>一个11位长的mobile字符串对象占用了64字节,于是user对象变成了 40+64=104 字节</p> <pre> <code class="language-java">// 给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]}</code></pre> <p>可知一个Date对象占用了24字节, 于是全部属性不为空的一个User对象占用内存为128字节。</p> <p>另外还可以通过另外一个工具-- JOL (Java Object Layout) --可知更详细的 Footprint 信息</p> <p>通过上面的工具我们只是知道一个空User对象占用了 24 字节以及简单的</p> <pre> <code class="language-java">Footprint{Objects=1, References=3, Primitives=[]}</code></pre> <p>通过此工具可知这24个字节是怎么分配的了</p> <pre> <code class="language-java">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</code></pre> <p>上面我们知道一个Integer对象占用了16字节 看这16个字节是怎么分配的</p> <pre> <code class="language-java">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</code></pre> <p>上面我们知道一个11位长的String对象占用了64字节 看其是怎么分配的</p> <pre> <code class="language-java">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</code></pre> <p>即一个空String对象占用了24字节</p> <pre> <code class="language-java">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</code></pre> <p>一个长度为0的char数组占了16字节 于是11位长的char数组占用字节为: 16+2*11=38 因为需要按8字节对齐 于是还得加上2字节的填充符 于是变成了40字节。 所以一个11位长的字符串的占用字节为24+40=64</p> <h3><strong>补充</strong></h3> <p>memory-measurer 如何使用</p> <pre> <code class="language-java">git clone https://github.com/msteindorfer/memory-measurer cd memory-measurer mvn clean install</code></pre> <p>pom文件中添加依赖</p> <pre> <code class="language-java"><dependency> <groupId>com.github.msteindorfer</groupId> <artifactId>memory-measurer</artifactId> <version>0.1.0-SNAPSHOT</version> </dependency></code></pre> <p>运行时时显式添加vm参数 如</p> <pre> <code class="language-java">-javaagent:/Users/zhugw/workspace/memory-measurer/target/memory-measurer-0.1.0-SNAPSHOT.jar</code></pre> <p>jol使用说明</p> <p>只需添加依赖</p> <pre> <code class="language-java"><dependency> <groupId>org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> <version>0.6</version> </dependency></code></pre> <h3> </h3> <p> </p> <p>来自:https://segmentfault.com/a/1190000007183623</p> <p> </p>