sclass RecursiveObjectSize { long size; new SimpleStack stack; Set seen = new CompactIdentityHashSet; // add objects here to ignore them Set fieldsToIgnore; Set realmsToIgnore; swappable void onNewObject(O o) {} swappable void onSizeChanged() {} void incSize(long n) { size += n; onSizeChanged(); } void recurse(O o) { if (o == null) ret; stack.add(o); while ping (nempty(stack)) step(popLast(stack)); } void step(O o) { if (!seen.add(o)) ret; if (guessDeepObjectSize_shouldIgnoreObject(o)) ret; onNewObject(o); 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 && !seen.contains(x)) stack.add(x); } else len = inMemorySizeOfPrimitiveArray(o); size += len; ret; } incSize(unsafe_sizeOf(o)); if (sizeCalculation_shouldSkipObject(o)) ret; for (Field f : nonStaticNonPrimitiveFieldObjects(o)) { if (contains(fieldsToIgnore, f)) continue; O x; try { x = f.get(o); } catch e { print("Error trying to access " + f + ": " + e); continue; } if (x != null && !seen.contains(x)) { stack.add(x); ifdef guessDeepObjectSize_recursive_debug print("gos field " + f + ": " + getClassName(x)); endifdef } } //fail("can't handle object type: " + className(o)); } }