// I guess "transitive total object size" or something // would be a better name. sclass RecursiveObjectSize { long size; new SimpleStack stack; Set seen = new CompactIdentityHashSet; // add objects here to ignore them Set fieldsToIgnore; Set realmsToIgnore; settable int dummySizeForUnknownObject = 24; settable bool skipTransientFields; swappable void onNewObject(O o) {} swappable void onSizeChanged() {} swappable bool shouldIgnore(O o) { false; } void resize(int expectedObjectCount) { seen = new CompactIdentityHashSet(expectedObjectCount); } void incSize(long n) { size += n; onSizeChanged(); } long recurse(O o) { if (o == null) ret 0; seen.add(o); stack.add(o); while ping (nempty(stack)) step(popLast(stack)); ret size; } void step(O o) { if (guessDeepObjectSize_shouldIgnoreObject(o)) ret; if (shouldIgnore(o)) ret; onNewObject(o); if (realmsToIgnore != null && contains(realmsToIgnore, getRealm(o))) ret; ifdef RecursiveObjectSize_debug print("gos: " + getClassName(o)); endifdef if (o instanceof Class) ret; if (isInstanceOfSyntheticClass(o)) ret with incSize(dummySizeForUnknownObject()); incSize(unsafe_sizeOf(o)); if (o cast O[]) { for (O x : o) if (x != null && seen.add(x)) stack.add(x); ret; } if (sizeCalculation_shouldSkipObject(o)) ret; for (Field f : nonStaticNonPrimitiveFieldObjects(o)) { if (contains(fieldsToIgnore, f)) continue; if (skipTransientFields && isTransient(f)) continue; O x; try { x = f.get(o); } catch e { print("Error trying to access " + f + ": " + e); continue; } if (x != null && seen.add(x)) { stack.add(x); ifdef RecursiveObjectSize_debug print("gos field " + f + ": " + getClassName(x)); endifdef } } //fail("can't handle object type: " + className(o)); } bool hasSeen(O o) { ret seen.contains(o); } }