本文整理汇总了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;
}