java 内存和实际内存_Java对象的内存实际占用

//package net.sourceforge.sizeof;

import java.io.IOException;

import java.io.OutputStream;

import java.io.PrintStream;

import java.lang.instrument.Instrumentation;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.util.IdentityHashMap;

import java.util.Map;public classSizeOf {private static OutputStream out = System.out;private staticInstrumentation inst;private static long MIN_CLASS_SIZE_TO_LOG = 1048576L;private static boolean SKIP_STATIC_FIELD = false;private static boolean SKIP_FINAL_FIELD = false;private static boolean SKIP_FLYWEIGHT_FIELD = false;private static boolean debug = false;private static String[] unit = {"b", "Kb", "Mb", "Gb", "Tb"};public static voidpremain(String paramString, Instrumentation paramInstrumentation) {

inst=paramInstrumentation;

System.out.println("JAVAGENT: call premain instrumentation for class SizeOf");

}public static longsizeOf(Object paramObject) {if (inst == null) {throw new IllegalStateException("Instrumentation is null");

}if ((SKIP_FLYWEIGHT_FIELD) &&(isSharedFlyweight(paramObject))) {return 0L;

}returninst.getObjectSize(paramObject);

}public static String humanReadable(longparamLong) {double d =paramLong;int i = 0;for (i = 0; (i < 5) && (d >= 1024.0D); i++) {

d/= 1024.0D;

}return d +unit[i];

}public static longdeepSizeOf(Object paramObject) {

IdentityHashMap localIdentityHashMap= newIdentityHashMap();return deepSizeOf(paramObject, localIdentityHashMap, 0);

}/**

* @deprecated*/

public static longiterativeSizeOf(Object paramObject)

throws IllegalArgumentException, IllegalAccessException, IOException {returndeepSizeOf(paramObject);

}private static String indent(intparamInt) {

StringBuilder localStringBuilder= newStringBuilder();for (int i = 0; i < paramInt; i++) {

localStringBuilder.append(" ");

}returnlocalStringBuilder.toString();

}private static long deepSizeOf(Object paramObject, Map paramMap, intparamInt) {if (paramObject == null) {if(debug) {

print("null\n");

}return 0L;

}long l = 0L;if(paramMap.containsKey(paramObject)) {if(debug) {

print("\n%s{ yet computed }\n", newObject[]{indent(paramInt)});

}return 0L;

}if(debug) {

print("\n%s{ %s\n", newObject[]{indent(paramInt), paramObject.getClass().getName()});

}

paramMap.put(paramObject,null);

l=sizeOf(paramObject);if((paramObject instanceof Object[])) {int i = 0;for(Object localObject2 : (Object[]) paramObject) {if(debug) {

print("%s [%d] =", new Object[]{indent(paramInt), Integer.valueOf(i++)});

}

l+= deepSizeOf(localObject2, paramMap, paramInt + 1);

}

}else{

Field[] arrayOfField=paramObject.getClass().getDeclaredFields();for(Object localObject2 : arrayOfField) {

((Field) localObject2).setAccessible(true);

Object localObject3;try{

localObject3= ((Field) localObject2).get(paramObject);

}catch(IllegalArgumentException localIllegalArgumentException) {throw newRuntimeException(localIllegalArgumentException);

}catch(IllegalAccessException localIllegalAccessException) {throw newRuntimeException(localIllegalAccessException);

}if(isComputable((Field) localObject2)) {if(debug) {

print("%s %s =", newObject[]{indent(paramInt), ((Field) localObject2).getName()});

}

l+= deepSizeOf(localObject3, paramMap, paramInt + 1);

}else if(debug) {

print("%s %s = %s\n", newObject[]{indent(paramInt), ((Field) localObject2).getName(), localObject3.toString()});

}

}

}if(debug) {

print("%s} size = %s\n", newObject[]{indent(paramInt), humanReadable(l)});

}if ((MIN_CLASS_SIZE_TO_LOG > 0L) && (l >=MIN_CLASS_SIZE_TO_LOG)) {

print("Found big object: %s%s@%s size: %s\n", newObject[]{indent(paramInt), paramObject.getClass().getName(), Integer.valueOf(System.identityHashCode(paramObject)), humanReadable(l)});

}returnl;

}private staticboolean isAPrimitiveType(Class paramClass) {if (paramClass ==Boolean.TYPE) {return true;

}if (paramClass ==Character.TYPE) {return true;

}if (paramClass ==Byte.TYPE) {return true;

}if (paramClass ==Short.TYPE) {return true;

}if (paramClass ==Integer.TYPE) {return true;

}if (paramClass ==Long.TYPE) {return true;

}if (paramClass ==Float.TYPE) {return true;

}if (paramClass ==Double.TYPE) {return true;

}return paramClass ==Void.TYPE;

}private staticboolean isComputable(Field paramField) {int i =paramField.getModifiers();if(isAPrimitiveType(paramField.getType())) {return false;

}if ((SKIP_STATIC_FIELD) &&(Modifier.isStatic(i))) {return false;

}return (!SKIP_FINAL_FIELD) || (!Modifier.isFinal(i));

}private staticboolean isSharedFlyweight(Object paramObject) {if((paramObject instanceof Comparable)) {if((paramObject instanceof Enum)) {return true;

}if((paramObject instanceof String)) {return paramObject ==((String) paramObject).intern();

}if((paramObject instanceof Boolean)) {return (paramObject == Boolean.TRUE) || (paramObject ==Boolean.FALSE);

}if((paramObject instanceof Integer)) {return paramObject ==Integer.valueOf(((Integer) paramObject).intValue());

}if((paramObject instanceof Short)) {return paramObject ==Short.valueOf(((Short) paramObject).shortValue());

}if((paramObject instanceof Byte)) {return paramObject ==Byte.valueOf(((Byte) paramObject).byteValue());

}if((paramObject instanceof Long)) {return paramObject ==Long.valueOf(((Long) paramObject).longValue());

}if((paramObject instanceof Character)) {return paramObject ==Character.valueOf(((Character) paramObject).charValue());

}

}return false;

}public static void setMinSizeToLog(longparamLong) {

MIN_CLASS_SIZE_TO_LOG=paramLong;

}public static voidskipFinalField(boolean paramBoolean) {

SKIP_FINAL_FIELD=paramBoolean;

}public static voidskipStaticField(boolean paramBoolean) {

SKIP_STATIC_FIELD=paramBoolean;

}public static voidskipFlyweightObject(boolean paramBoolean) {

SKIP_FLYWEIGHT_FIELD=paramBoolean;

}private static voidprint(String paramString) {try{out.write(paramString.getBytes());

}catch(IOException localIOException) {throw newRuntimeException(localIOException);

}

}private static voidprint(String paramString, Object... paramVarArgs) {try{out.write(String.format(paramString, paramVarArgs).getBytes());

}catch(IOException localIOException) {throw newRuntimeException(localIOException);

}

}public static voidsetLogOutputStream(OutputStream paramOutputStream) {if (paramOutputStream == null) {throw new IllegalArgumentException("Can't use a null OutputStream");

}out =paramOutputStream;

}public static voidturnOnDebug() {

debug= true;

}public static voidturnOffDebug() {

debug= false;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值