!7 lib 1400078 // heap analyzer import org.netbeans.lib.profiler.heap.*; import java.lang.reflect.Field; static Heap heap; sbool fake = true; p-experiment { File f = stefansOS_heapDumpFile(); print("Loading heap dump (" + toM(fileSize(f)) + " M)..."); heap = HeapFactory.createHeap(f); print("Got heap."); L classes = heap.getAllClasses(); print("Got " + n2(classes, "class", "classes")); //pnl(sortedIC(mapMethod(classes, 'getName))); L classLoaderClasses = filter(classes, func(JavaClass c) -> bool { startsWithOneOf(c.getName(), "main$JavaXClassLoader", "x30$JavaXClassLoader") }); print("Got " + n2(classLoaderClasses, "class loader class", "class loader classes")); L classLoaders = concatLists(mapMethod('getInstances, classLoaderClasses)); print("Got " + n2(classLoaders, "class loader")); L defunctClassLoaders = filter(classLoaders, func(Instance cl) { isTrue(cl.getValueOfField('defunct)) }); print("Got " + n2(defunctClassLoaders, "defunct class loader")); if (fake && empty(defunctClassLoaders)) { defunctClassLoaders = classLoaders; print("FAKING IT"); } print("Calculating GC roots..."); for (Instance cl : defunctClassLoaders) { print("Path length to GC root: " + lengthOfPathToGCRoot(cl)); print("Path: " + joinWithSpace(mapMethod('getName, pathToGCRoot(cl)))); //Instance i = cl.getNearestGCRootPointer(); //print("i => " + i); } } static L pathToGCRoot(Instance i) { new L path; while licensed { path.add(i); Instance j = i.getNearestGCRootPointer(); if (j == null) null; // No path to GC root? if (j == i) break; i = j; } ret path; } static int lengthOfPathToGCRoot(Instance i) { int n = 0; while licensed { if (i == null) ret -1; // No path to GC root? Instance j = i.getNearestGCRootPointer(); if (j == i) ret n; i = j; ++n; } ret -2; // dummy when program got unlicensed }