标签:
原生类型(primitive type)的内存占用如下:
Primitive Type | Memory Required(bytes) |
boolean | 1 |
byte | 1 |
short | 2 |
char | 2 |
int | 4 |
float | 4 |
long | 8 |
double | 8 |
SizeOfAgent: 计算对象大小类
package com.wenniuwuren.objectsizeof; import java.lang.instrument.Instrumentation; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.IdentityHashMap; import java.util.Map; import java.util.Stack; /** * 借助 Instrumentation 接口的 getObjectSize 方法计算对象占用空间 * 原来的 sizeOf 只能计算本对象占用空间, 无法计算继承下来的占用空间, * 不过可以用反射的方法把全部占用空间计算出来 * * Created by zhuyb on 16/3/20. */ public class SizeOfAgent { static Instrumentation instrumentation; // 第一个参数由 –javaagent, 第二个参数由 JVM 传入 public static void premain(String agentArgs, Instrumentation instP) { instrumentation = instP; } // 返回没有子类对象大小的大小 public static long sizeOf(Object o) { if (instrumentation == null) { throw new IllegalStateException("Can not access instrumentation environment.\n" + "Please check if jar file containing SizeOfAgent class is \n" + "specified in the java's \"-javaagent\" command line argument."); } return instrumentation.getObjectSize(o); } /** * * 计算复合对象 * @param obj object to calculate size of * @return object size */ public static long fullSizeOf(Object obj) { Map<Object, Object> visited = new IdentityHashMap<Object, Object>(); Stack<Object> stack = new Stack<Object>(); long result = internalSizeOf(obj, stack, visited); while (!stack.isEmpty()) { result += internalSizeOf(stack.pop(), stack, visited); } visited.clear(); return result; } // 这个算法使每个对象仅被计算一次, 避免循环引用,即死循环计算 private static boolean skipObject(Object obj, Map<Object, Object> visited) { if (obj instanceof String) { // String 池里已有的不再计算 if (obj == ((String) obj).intern()) { return true; } } return (obj == null) // 已有对象不再计算 || visited.containsKey(obj); } private static long internalSizeOf(Object obj, Stack<Object> stack, Map<Object, Object> visited) { if (skipObject(obj, visited)){ return 0; } visited.put(obj, null); long result = 0; // get size of object + primitive variables + member pointers result += SizeOfAgent.sizeOf(obj); // 处理所有数组内容 Class clazz = obj.getClass(); if (clazz.isArray()) { // [I , [F 基本类型名字长度是2 if(clazz.getName().length() != 2) {// skip primitive type array int length = Array.getLength(obj); for (int i = 0; i < length; i++) { stack.add(Array.get(obj, i)); } } return result; } // 处理对象的所有字段 while (clazz != null) { Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { // 不重复计算静态类型字段 if (!Modifier.isStatic(fields[i].getModifiers())) { // 不重复计算原始类型字段 if (fields[i].getType().isPrimitive()) { continue; } else { // 使 private 属性可访问 fields[i].setAccessible(true); try { // objects to be estimated are put to stack Object objectToAdd = fields[i].get(obj); if (objectToAdd != null) { stack.add(objectToAdd); } } catch (IllegalAccessException ex) { assert false; } } } } clazz = clazz.getSuperclass(); } return result; } }
<!-- pom打jar包的时候设置MANIFEST.MF的key/value,可以通过在pom.xml文件中添加plugin的方式来实现 --> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <finalName>SizeOfAgent</finalName> <archive> <manifestEntries> <Premain-class>com.wenniuwuren.objectsizeof.SizeOfAgent</Premain-class> <Boot-Class-Path></Boot-Class-Path> <Can-Redefine-Classes>false</Can-Redefine-Classes> </manifestEntries> <addMavenDescriptor>false</addMavenDescriptor> </archive> </configuration> </plugin> </plugins> </build>
package com.wenniuwuren.objectsizeof; import static com.wenniuwuren.objectsizeof.SizeOfAgent.*; /** * 以下结果在 64-bit JVM 下测试 * 启动参数1(不压缩指针长度):-javaagent:target/SizeOfAgent.jar -XX:-UseCompressedOops * * Created by zhuyb on 16/3/20. */ public class SizeOfAgentTest { public static void main(String[] args) { System.out.println("------------------空对象----------------------------"); // 16 bytes + 0 + 0 = 16 空对象, 只有对象头 System.out.println("sizeOf(new Object()) = " + sizeOf(new Object())); System.out.println("fullSizeOf(new Object()) = " + fullSizeOf(new Object())); System.out.println("----------------非空对象含有原始类型、引用类型------------------------------"); // 16 bytes + 8 + 4 + padding = 32 System.out.println("sizeOf(new A()) = " + sizeOf(new A())); System.out.println("fullSizeOf(new A()) = " + fullSizeOf(new A())); // 16 + 4 + padding =24 数据是一个 int System.out.println("sizeOf(new Integer(1)) = " + sizeOf(new Integer(1))); // (16 + int hash:4 + int hash32:4 + refer char value[]:8 + padding) = 32 // 静态属性(static)不计算空间,因为所有对象都是共享一块空间的 // 不同版本JDK可能 String 内部 Field 可能不同,本次测试使用JDK1.7 System.out.println("sizeOf(new String()) = " + sizeOf(new String())); // (16 + 4 + 4 + 8 + padding) + (24 + 0 + padding) = 56 System.out.println("fullSizeOf(new String()) = " + fullSizeOf(new String())); // (16 + 4 + 4 + 8 + padding) = 32 System.out.println("sizeOf(new String('a')) = " + sizeOf(new String("a"))); // (16 + 4 + 4 + 8 +padding) + (24 + 2 + padding) = 64 System.out.println("fullSizeOf(new String('a')) = " + fullSizeOf(new String("a"))); System.out.println("-------------------原始类型数组对象---------------------------"); // 24 bytes + 0*1 + 0 = 24 数组长度为 0,所以只有对象头的长度 System.out.println("sizeOf(new byte[0]) = " + sizeOf(new byte[0])); System.out.println("fullSizeOf(new byte[0]) = " + fullSizeOf(new byte[0])); // 24 + 1*1 + padding = 32 System.out.println("sizeOf(new byte[1]) = " + sizeOf(new byte[1])); System.out.println("fullSizeOf(new byte[1]) = " + fullSizeOf(new byte[1])); // 24 + 1*2 + padding = 32 System.out.println("sizeOf(new char[1]) = " + sizeOf(new char[1])); System.out.println("fullSizeOf(new char[1]) = " + fullSizeOf(new char[1])); // 24 + 9*1 + padding = 40 System.out.println("sizeOf(new byte[9]) = " + sizeOf(new byte[9])); System.out.println("fullSizeOf(new byte[9]) = " + fullSizeOf(new byte[9])); System.out.println("--------------------引用类型数组对象--------------------------"); // 24 bytes + 0*8 + 0 = 24 数组长度为 0 System.out.println("sizeOf(new Integer[0]) = " + sizeOf(new Integer[0])); System.out.println("fullSizeOf(new Integer[0]) = " + fullSizeOf(new Integer[0])); // 24 bytes + 1*8 + 0 = 32 引用对象 64-bit JVM 占用 8 bytes System.out.println("sizeOf(new Integer[1]) = " + sizeOf(new Integer[1])); System.out.println("fullSizeOf(new Integer[1]) = " + fullSizeOf(new Integer[1])); // 24 bytes + 2*8 + padding = 40 System.out.println("sizeOf(new Integer[1]) = " + sizeOf(new Integer[1])); System.out.println("fullSizeOf(new Integer[1]) = " + fullSizeOf(new Integer[1])); // 24 + 3*8 + padding = 48 System.out.println("sizeOf(new Integer[3]) = " + sizeOf(new Integer[3])); System.out.println("fullSizeOf(new Integer[3]) = " + fullSizeOf(new Integer[3])); System.out.println("-------------------自定义数组对象---------------------------"); // 16 + (4+8) + padding = 32 System.out.println("sizeOf(new B()) = " + sizeOf(new B())); System.out.println("fullSizeOf(new B()) = " + fullSizeOf(new B())); // 24 + 0*8 + padding = 24 引用对象 64-bit JVM 占用 8 bytes, // 因为没创建真实的 new B()所以 B类内部数据还未占用空间 System.out.println("sizeOf(new B[0]) = " + sizeOf(new B[0])); System.out.println("fullSizeOf(new B[0]) = " + fullSizeOf(new B[0])); // 24 + 1*8 + padding = 32 System.out.println("sizeOf(new B[1]) = " + sizeOf(new B[1])); System.out.println("fullSizeOf(new B[1]) = " + fullSizeOf(new B[1])); // 24 + 2*8 + padding = 40 System.out.println("sizeOf(new B[2]) = " + sizeOf(new B[2])); System.out.println("fullSizeOf(new B[2]) = " + fullSizeOf(new B[2])); // 24 + 3*8 + padding = 48 System.out.println("sizeOf(new B[3]) = " + sizeOf(new B[3])); System.out.println("fullSizeOf(new B[3]) = " + fullSizeOf(new B[3])); System.out.println("-------------------复合对象---------------------------"); // 16 + (4+8) + padding = 32 sizeOf 只计算单层次占用空间大小 System.out.println("sizeOf(new C()) = " + sizeOf(new C())); // (16 + (4+8) + padding1) + (24 + 2*8 + padding2) + 2*(16 + (4+8) + padding3) = 136 // 递归计算当前对象占用空间总大小,包括当前类和超类的实例字段大小以及实例字段引用对象大小 System.out.println("fullSizeOf(new C()) = " + fullSizeOf(new C())); System.out.println("-------------------继承关系---------------------------"); // 涉及继承关系的时候有一个最基本的规则:首先存放父类中的成员,接着才是子类中的成员, 父类也要按照 8 byte 规定 // 16 + 1 + padding = 24 System.out.println("sizeOf(new D()) = " + sizeOf(new D())); System.out.println("fullSizeOf(new D()) = " + fullSizeOf(new D())); // 16 + 父类(1 + padding1) + 1 + padding2 = 32 System.out.println("sizeOf(new E()) = " + sizeOf(new E())); System.out.println("fullSizeOf(new E()) = " + fullSizeOf(new E())); } public static class A { int a; Integer b; } public static class B { int a; Integer b; } public static class C{ int c; B[] b = new B[2]; // 初始化 C() { for (int i = 0; i < b.length; i++) { b[i] = new B(); } } } public static class D { byte d1; } public static class E extends D { byte e1; } }
------------------空对象---------------------------- sizeOf(new Object()) = 16 fullSizeOf(new Object()) = 16 ----------------非空对象含有原始类型、引用类型------------------------------ sizeOf(new A()) = 32 fullSizeOf(new A()) = 32 sizeOf(new Integer(1)) = 24 sizeOf(new String()) = 32 fullSizeOf(new String()) = 56 sizeOf(new String('a')) = 32 fullSizeOf(new String('a')) = 64 -------------------原始类型数组对象--------------------------- sizeOf(new byte[0]) = 24 fullSizeOf(new byte[0]) = 24 sizeOf(new byte[1]) = 32 fullSizeOf(new byte[1]) = 32 sizeOf(new char[1]) = 32 fullSizeOf(new char[1]) = 32 sizeOf(new byte[9]) = 40 fullSizeOf(new byte[9]) = 40 --------------------引用类型数组对象-------------------------- sizeOf(new Integer[0]) = 24 fullSizeOf(new Integer[0]) = 24 sizeOf(new Integer[1]) = 32 fullSizeOf(new Integer[1]) = 32 sizeOf(new Integer[1]) = 32 fullSizeOf(new Integer[1]) = 32 sizeOf(new Integer[3]) = 48 fullSizeOf(new Integer[3]) = 48 -------------------自定义数组对象--------------------------- sizeOf(new B()) = 32 fullSizeOf(new B()) = 32 sizeOf(new B[0]) = 24 fullSizeOf(new B[0]) = 24 sizeOf(new B[1]) = 32 fullSizeOf(new B[1]) = 32 sizeOf(new B[2]) = 40 fullSizeOf(new B[2]) = 40 sizeOf(new B[3]) = 48 fullSizeOf(new B[3]) = 48 -------------------复合对象--------------------------- sizeOf(new C()) = 48 fullSizeOf(new C()) = 152 -------------------继承关系--------------------------- sizeOf(new D()) = 24 fullSizeOf(new D()) = 24 sizeOf(new E()) = 32 fullSizeOf(new E()) = 32
标签:
原文地址:http://blog.csdn.net/wenniuwuren/article/details/50958892