sbool cleanUp_interruptThreads; // experimental static void cleanUp(O c) { if (c == null) ret; if (c cast AutoCloseable) ret with close_pcall(c); if (c cast java.util.Timer) ret with c.cancel(); if (c cast Collection) { cleanUp(c); ret; } if (c cast Map) { for (O o : keysList(c)) cleanUp(o); for (O o : valuesList(c)) cleanUp(o); syncClear(c); ret; } //if (!(c instanceof Class)) ret; pcall { // revoke license preCleanUp(c); // unpause setOpt_raw(c, "ping_pauseAll", false); // call custom cleanMeUp() and cleanMeUp_*() functions innerCleanUp(c); // Java spec says finalize should only be called by GC, // but we care to differ. // Edit: Not anymore (illegal access warnings) /*if (isTrue(vmMap_get('callFinalize))) pcallOpt(c, "finalize");*/ // remove all virtual bots (hope this works) L androids = (L) getOpt(c, "record_list"); for (O android : unnull(androids)) pcallOpt(android, "dispose"); // heck we'll dispose anything // sub-cleanup L classes = cast getOpt(c, "hotwire_classes"); if (classes != null) for (WeakReference cc : classes) pcall { cleanUp(cc.get()); } // interrupt all threads (experimental, they might be doing cleanup?) if (cleanUp_interruptThreads) { L threads = registeredThreads(c); if (nempty(threads)) { print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads))); interruptThreads(threads); } } } setOpt_raw(c, cleaningUp_flag := false); if (c instanceof Class && ((Class) c).getName().equals("main")) retireClassLoader(((Class) c).getClassLoader()); } static void cleanUp(Collection l) { if (l == null) ret; for (O c : l) cleanUp(c); l.clear(); }