这篇说说如何计算Java对象大小的方法。之前在聊聊高并发(四)Java对象的表示模型和运行时内存表示 这篇中已经说了Java对象的内存表示模型是Oop-Klass模型。
普通对象的结构如下,按64位机器的长度计算
1. 对象头(_mark), 8个字节
2. Oop指针,如果是32G内存以下的,默认开启对象指针压缩,4个字节
3. 数据区
4.Padding(内存对齐),按照8的倍数对齐
数组对象结构是
1. 对象头(_mark), 8个字节
2. Oop指针,如果是32G内存以下的,默认开启对象指针压缩,4个字节
3. 数组长度,4个字节
4. 数据区
5. Padding(内存对齐),按照8的倍数对齐
清楚了对象在内存的基本布局后,咱们说两种计算Java对象大小的方法
1. 通过java.lang.instrument.Instrumentation的getObjectSize(obj)直接获取对象的大小
2. 通过sun.misc.Unsafe对象的objectFieldOffset(field)等方法结合反射来计算对象的大小
java.lang.instrument.Instrumentation.getObjectSize()的方式
先讲讲java.lang.instrument.Instrumentation.getObjectSize()的方式,这种方法得到的是Shallow Size,即遇到引用时,只计算引用的长度,不计算所引用的对象的实际大小。如果要计算所引用对象的实际大小,可以通过递归的方式去计算。
java.lang.instrument.Instrumentation的实例必须通过指定javaagent的方式才能获得,具体的步骤如下:
1. 定义一个类,提供一个premain方法: public static void premain(String agentArgs, Instrumentation instP)
2. 创建META-INF/MANIFEST.MF文件,内容是指定PreMain的类是哪个: Premain-Class: sizeof.ObjectShallowSize
3. 把这个类打成jar,然后用java -javaagent XXXX.jar XXX.main的方式执行
下面先定义一个类来获得java.lang.instrument.Instrumentation的实例,并提供了一个static的sizeOf方法对外提供Instrumentation的能力
package sizeof;
import java.lang.instrument.Instrumentation;
public class ObjectShallowSize {
private static Instrumentation inst;
public static void premain(String agentArgs, Instrumentation instP){
inst = instP;
}
public static long sizeOf(Object obj){
return inst.getObjectSize(obj);
}
}
定义META-INF/MANIFEST.MF文件
Premain-Class: sizeof.ObjectShallowSize
cd 编译后的类和META-INF文件夹所在目录
jar cvfm java-agent-sizeof.jar META-INF/MANIFEST.MF .
有如下这个类,字段的定义按如下顺序
private static class ObjectA {
String str; // 4
int i1; // 4
byte b1; // 1
byte b2; // 1
int i2; // 4
ObjectB obj; //4
byte b3; // 1
}
8(_mark) + 4(oop指针) + 4(str) + 4(i1) + 1(b1) + 1(b2) + 2(padding) + 4(i2) + 4(obj) + 1(b3) + 7(padding) = 40 ?
但事实上是这样的吗? 我们来用Instrumentation的getObjectSize来计算一下先:
package test;
import sizeof.ObjectShallowSize;
public class SizeofWithInstrumetation {
private static class ObjectA {
String str; // 4
int i1; // 4
byte b1; // 1
byte b2; // 1
int i2; // 4
ObjectB obj; //4
byte b3; // 1
}
private static class ObjectB {
}
public static void main(String[] args){
System.out.println(ObjectShallowSize.sizeOf(new ObjectA()));
}
}
得到的结果是32!不是会按8对齐吗,b3之前的数据加起来已经是32了,多了1个b3,为33,应该对齐到40才对啊。事实上,HotSpot创建的对象的字段会先按照给定顺序排列一下,默认的顺序如下,从长到短排列,引用排最后: long/double --> int/float --> short/char --> byte/boolean --> Reference
这个顺序可以使用JVM参数: -XX:FieldsAllocationSylte=0(默认是1)来改变。
我们使用sun.misc.Unsafe对象的objectFieldOffset方法来验证一下:
Field[] fields = ObjectA.class.getDeclaredFields();
for(Field f: fields){
System.out.println(f.getName() + " offset: " +unsafe.objectFieldOffset(f));
}
可以看到确实是按照从长到短,引用排最后的方式在内存中排列的。按照这种方法我们来重新计算下ObjectA创建的对象的长度:
8(_mark) + 4(oop指针) + 4(i1) + + 4(i2) + 1(b1) + 1(b2) + 1(b3) + 1(padding) + 4(str) + 4(obj) = 32
得到的结果和java.lang.instrument.Instrumentation.getObjectSize()的结果是一样的,证明我们的计算方式是正确的。
sun.misc.Unsafe的方式
下面说一下通过sun.misc.Unsafe对象的objectFieldOffset(field)等方法结合反射来计算对象的大小。基本的思路如下:
1. 通过反射获得一个类的Field
2. 通过Unsafe的objectFieldOffset()获得每个Field的offSet
3. 对Field按照offset排序,取得最大的offset,然后加上这个field的长度,再加上Padding对齐
上面三步就可以获得一个对象的Shallow size。可以进一步通过递归去计算所引用对象的大小,从而可以计算出一个对象所占用的实际大小。
如何获得Unsafe对象已经在这篇中聊聊序列化(二)使用sun.misc.Unsafe绕过new机制来创建Java对象说过了,可以通过反射的机制来获得.
Oop指针是4还是未压缩的8也可以通过unsafe.arrayIndexScale(Object[].class)来获得,这个方法返回一个引用所占用的长度
static {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
objectRefSize = unsafe.arrayIndexScale(Object[].class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* This class contains object info generated by ClassIntrospector tool
*/
public class ObjectInfo {
/** Field name */
public final String name;
/** Field type name */
public final String type;
/** Field data formatted as string */
public final String contents;
/** Field offset from the start of parent object */
public final int offset;
/** Memory occupied by this field */
public final int length;
/** Offset of the first cell in the array */
public final int arrayBase;
/** Size of a cell in the array */
public final int arrayElementSize;
/** Memory occupied by underlying array (shallow), if this is array type */
public final int arraySize;
/** This object fields */
public final List children;
public ObjectInfo(String name, String type, String contents, int offset, int length, int arraySize,
int arrayBase, int arrayElementSize)
{
this.name = name;
this.type = type;
this.contents = contents;
this.offset = offset;
this.length = length;
this.arraySize = arraySize;
this.arrayBase = arrayBase;
this.arrayElementSize = arrayElementSize;
children = new ArrayList( 1 );
}
public void addChild( final ObjectInfo info )
{
if ( info != null )
children.add( info );
}
/**
* Get the full amount of memory occupied by a given object. This value may be slightly less than
* an actual value because we don't worry about memory alignment - possible padding after the last object field.
*
* The result is equal to the last field offset + last field length + all array sizes + all child objects deep sizes
* @return Deep object size
*/
public long getDeepSize()
{
//return length + arraySize + getUnderlyingSize( arraySize != 0 );
return addPaddingSize(arraySize + getUnderlyingSize( arraySize != 0 ));
}
long size = 0;
private long getUnderlyingSize( final boolean isArray )
{
//long size = 0;
for ( final ObjectInfo child : children )
size += child.arraySize + child.getUnderlyingSize( child.arraySize != 0 );
if ( !isArray && !children.isEmpty() ){
int tempSize = children.get( children.size() - 1 ).offset + children.get( children.size() - 1 ).length;
size += addPaddingSize(tempSize);
}
return size;
}
private static final class OffsetComparator implements Comparator
{
@Override
public int compare( final ObjectInfo o1, final ObjectInfo o2 )
{
return o1.offset - o2.offset; //safe because offsets are small non-negative numbers
}
}
//sort all children by their offset
public void sort()
{
Collections.sort( children, new OffsetComparator() );
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
toStringHelper( sb, 0 );
return sb.toString();
}
private void toStringHelper( final StringBuilder sb, final int depth )
{
depth( sb, depth ).append("name=").append( name ).append(", type=").append( type )
.append( ", contents=").append( contents ).append(", offset=").append( offset )
.append(", length=").append( length );
if ( arraySize > 0 )
{
sb.append(", arrayBase=").append( arrayBase );
sb.append(", arrayElemSize=").append( arrayElementSize );
sb.append( ", arraySize=").append( arraySize );
}
for ( final ObjectInfo child : children )
{
sb.append( '\n' );
child.toStringHelper(sb, depth + 1);
}
}
private StringBuilder depth( final StringBuilder sb, final int depth )
{
for ( int i = 0; i < depth; ++i )
sb.append( "\t");
return sb;
}
private long addPaddingSize(long size){
if(size % 8 != 0){
return (size / 8 + 1) * 8;
}
return size;
}
}
package test;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import sun.misc.Unsafe;
/**
* This class could be used for any object contents/memory layout printing.
*/
public class ClassIntrospector {
private static final Unsafe unsafe;
/** Size of any Object reference */
private static final int objectRefSize;
static {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
objectRefSize = unsafe.arrayIndexScale(Object[].class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/** Sizes of all primitive values */
private static final Map primitiveSizes;
static {
primitiveSizes = new HashMap(10);
primitiveSizes.put(byte.class, 1);
primitiveSizes.put(char.class, 2);
primitiveSizes.put(int.class, 4);
primitiveSizes.put(long.class, 8);
primitiveSizes.put(float.class, 4);
primitiveSizes.put(double.class, 8);
primitiveSizes.put(boolean.class, 1);
}
/**
* Get object information for any Java object. Do not pass primitives to
* this method because they will boxed and the information you will get will
* be related to a boxed version of your value.
*
* @param obj
* Object to introspect
* @return Object info
* @throws IllegalAccessException
*/
public ObjectInfo introspect(final Object obj)
throws IllegalAccessException {
try {
return introspect(obj, null);
} finally { // clean visited cache before returning in order to make
// this object reusable
m_visited.clear();
}
}
// we need to keep track of already visited objects in order to support
// cycles in the object graphs
private IdentityHashMap
先一个测试类来验证一下Unsafe的方式计算出的结果
public class ClassIntrospectorTest
{
public static void main(String[] args) throws IllegalAccessException {
final ClassIntrospector ci = new ClassIntrospector();
ObjectInfo res;
res = ci.introspect( new ObjectA() );
System.out.println( res.getDeepSize() );
}
private static class ObjectA {
String str; // 4
int i1; // 4
byte b1; // 1
byte b2; // 1
int i2; // 4
ObjectB obj; //4
byte b3; // 1
}
private static class ObjectB {
}
}
32
和我们之前计算结果是一致的,证明是正确的。
最后再来测试一下数组对象的长度。有两个类如下:
private static class ObjectC {
ObjectD[] array = new ObjectD[2];
}
private static class ObjectD {
int value;
}
它们在内存的大体分布如下图:
我们可以手工计算一下ObjectC obj = new ObjectC()的大小:
ObjectC的Shallow size = 8(_mark) + 4(oop指针) + 4(ObjectD[]引用) = 16
new ObjectD[2]数组的长度 = 8(_mark) + 4(oop指针) + 4(数组长度占4个字节) + 4(ObjectD[0]引用) + 4(ObjectD[1]引用) = 24
由于ObjectD[]数组没有指向具体的对象大小,所以我们手工计算的结果是16 + 24 = 40
使用Unsafe对象的方式来计算一下:
public static void main(String[] args) throws IllegalAccessException {
final ClassIntrospector ci = new ClassIntrospector();
ObjectInfo res;
res = ci.introspect( new ObjectC() );
System.out.println( res.getDeepSize() );
}
40
再给ObjectD[]数组指向具体的ObjectD对象,再测试一下结果:
public static void main(String[] args) throws IllegalAccessException {
final ClassIntrospector ci = new ClassIntrospector();
ObjectInfo res;
res = ci.introspect( new ObjectC() );
System.out.println( res.getDeepSize() );
}
private static class ObjectC {
ObjectD[] array = new ObjectD[2];
public ObjectC(){
array[0] = new ObjectD();
array[1] = new ObjectD();
}
}
private static class ObjectD {
int value;
}
ObjectC的Shallow size = 8(_mark) + 4(oop指针) + 4(ObjectD[]引用) = 16
new ObjectD[2]数组的长度 = 8(_mark) + 4(oop指针) + 4(数组长度占4个字节) + 4(ObjectD[0]引用) + 4(ObjectD[1]引用) = 24
ObjectD对象长度 = 8(_mark) + 4(oop指针) + 4(value) = 16
所以ObjectC实际占用的空间 = 16 + 24 + 2 * 16 = 72
使用Unsafe的方式计算的结果也是72,和我们手工计算的方式一致。
参考: Memory introspection using sun.misc.Unsafe and reflection