!7 lib 1400078 // heap analyzer import org.gridkit.jvmtool.heapdump.*; import org.netbeans.lib.profiler.heap.*; import java.lang.reflect.Field; static Heap heap; sbool fake = false; sbool java10 = 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"); } else { print("No memory leaks."); ret; } print("Calculating GC roots..."); for (Instance cl : defunctClassLoaders) { print("Checking module ID: " + stringValue((Instance) cl.getValueOfField('progID))); print("Path length to GC root: " + lengthOfPathToGCRoot(cl)); print("Path: " + joinWithSpace(map(func(Instance i) -> S { i.getJavaClass().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 } sS stringValue(Instance obj) { ret java10 ? stringValue_java10(obj) : HeapWalker.stringValue(obj); } // TODO: auto-revert to Java 8 method static S stringValue_java10(Instance obj) { if (obj == null) null; if (!"java.lang.String".equals(obj.getJavaClass().getName())) throw new IllegalArgumentException("Is not a string: " + obj.getInstanceId() + " (" + obj.getJavaClass().getName() + ")"); int UTF16 = 1; bool COMPACT_STRINGS = true; // How to find out? O valueInstance = obj.getValueOfField("value"); byte coderField = cast obj.getValueOfField("coder"); byte coder = COMPACT_STRINGS ? coderField : (byte) UTF16; print("coder: " + coder); PrimitiveArrayInstance chars = cast valueInstance; int len = chars.getLength() >> coder; char[] text = new char[len]; L values = (L) chars.getValues(); if (coder == UTF16) for (int i = 0; i < text.length; i++) text[i] = values.get(i).charAt(0); else for (int i = 0; i < text.length; i++) text[i] = (char) parseInt(values.get(i)); ret print("got string: ", new String(text)); }