本文整理匯總了Java中java.lang.instrument.Instrumentation.getObjectSize方法的典型用法代碼示例。如果您正苦於以下問題:Java Instrumentation.getObjectSize方法的具體用法?Java Instrumentation.getObjectSize怎麽用?Java Instrumentation.getObjectSize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.instrument.Instrumentation
的用法示例。
在下文中一共展示了Instrumentation.getObjectSize方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getArrayDeepSize
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
private long getArrayDeepSize(Instrumentation instr, Object[] array, Set<Object> exclude) {
long size = instr.getObjectSize(array);
for (Object element : array) {
if (element == null) continue;
size += getObjectDeepSize(instr, array, exclude);
}
return size;
}
示例2: getObjectDeepSize
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
private long getObjectDeepSize(Instrumentation instr, Object object, Set<Object> exclude) {
if (exclude == null) {
exclude = new HashSet<>();
}
if (!exclude.add(object)) {
// System.out.println("skipping " + object);
return 0; // already calculated
}
Class klass = object.getClass();
if (klass == Class.class || klass.isEnum()) {
return 0;
}
if (klass.isArray()) {
if (klass.getComponentType().isPrimitive()) {
// primitive array size is straightforward
System.out.println(object + " is a primitive array, bytes: " + instr.getObjectSize(object));
return instr.getObjectSize(object);
}
// calculate object array deep size
long deep = getArrayDeepSize(instr, (Object[]) object, exclude);
System.out.println(object + " is object array, bytes: " + deep);
return deep;
}
// calculate object deep size
long size = instr.getObjectSize(object);
Field[] fields = klass.getDeclaredFields();
// System.out.println(object + " is " + klass);
for (Field f : fields) {
if ((f.getModifiers() & Modifier.STATIC) == Modifier.STATIC) continue;
Class type = f.getType();
if (type.isPrimitive()) continue;
try {
f.setAccessible(true);
Object o = f.get(object);
// System.out.println(f + " = " + o);
if (o == null) continue;
size += getObjectDeepSize(instr, o, exclude);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
return size;
}