sclass guessDeepObjectSize_recursive_Data { long size; Set stack = identityHashSet(); Set seen = identityHashSet(); // add objects here to ignore them Set fieldsToIgnore; Set realmsToIgnore; } static long guessDeepObjectSize_recursive(O o) { if (o == null) ret 0; ret guessDeepObjectSize_recursive(new guessDeepObjectSize_recursive_Data, o); } static long guessDeepObjectSize_recursive(guessDeepObjectSize_recursive_Data data, O o) { if (o == null) ret 0; data.stack.add(o); while ping (nempty(data.stack)) { /*ifdef guessDeepObjectSize_recursive_debug print("gos stack size: " + l(data.stack)); endifdef*/ guessDeepObjectSize_recursive_step(data, popFirst(data.stack)); } ret data.size; } svoid guessDeepObjectSize_recursive_step(guessDeepObjectSize_recursive_Data data, O o) { if (!data.seen.add(o)) ret; if (o instanceof Component) ret; // skipping AWT for now if (o instanceof Thread) ret; if (realmsToIgnore != null && contains(realmsToIgnore, getRealm(o))) ret; ifdef guessDeepObjectSize_recursive_debug print("gos: " + getClassName(o)); endifdef if (o instanceof Class) ret; if (isArray(o)) { int len; if (o instanceof O[]) { // len is computed correctly by unsafe_sizeOf len = unsafe_sizeOf(o); for (O x : (O[]) o) if (x != null && !data.seen.contains(x)) data.stack.add(x); } else len = roundUpTo(javaDataModelWordSize(), unsafe_sizeOf(_SimulatedArrayStub) + primitiveTypeSize(arrayElementType(o))*l(o)); data.size += len; ret; } data.size += unsafe_sizeOf(o); for (Field f : nonStaticNonPrimitiveFieldObjects(o)) pcall-short { if (contains(data.fieldsToIgnore, f)) continue; O x = f.get(o); if (x != null && !data.seen.contains(x)) { data.stack.add(x); ifdef guessDeepObjectSize_recursive_debug print("gos field " + f + ": " + getClassName(x)); endifdef } } //fail("can't handle object type: " + className(o)); }