publicstatic WeakReference<FinalizerTest> makeRef() {
FinalizerTest ft = new FinalizerTest("wahoo");
WeakReference<FinalizerTest> ref = new WeakReference<FinalizerTest>(ft);
ft = null; return ref;
}
publicstaticvoid main(String[] args) {
WeakReference<FinalizerTest> wimp = makeRef(); // Reference ft so we are sure the WeakReference cannot be cleared. // Note: This is very fragile. It was previously in a helper function but that // doesn't work for JIT-on-first-use with --gcstress where the object would be // collected when JIT internally allocates an array. Also adding a scope around // the keepLive lifetime somehow keeps a non-null `keepLive` around and makes // the test fail (even when keeping the `null` assignment). b/76454261
FinalizerTest keepLive = wimp.get();
System.out.println("wimp: " + wimpString(wimp));
Reference.reachabilityFence(keepLive);
keepLive = null; // Clear the reference.
/* this will try to collect and finalize ft */
System.out.println("gc");
Runtime.getRuntime().gc();
System.out.println("reborn: " + FinalizerTest.mReborn);
System.out.println("wimp: " + wimpString(wimp)); // Test runFinalization with multiple objects.
runFinalizationTest();
}
staticclass FinalizeCounter { publicstaticfinalint maxCount = 1024; publicstaticboolean finalized[] = newboolean[maxCount]; privatestatic Object finalizeLock = new Object(); privatestaticvolatileint finalizeCount = 0; privateint index; staticint getCount() { return finalizeCount;
} staticvoid printNonFinalized() { for (int i = 0; i < maxCount; ++i) { if (!FinalizeCounter.finalized[i]) {
System.out.println("Element " + i + " was not finalized");
}
}
}
FinalizeCounter(int index) { this.index = index;
} protectedvoid finalize() { synchronized(finalizeLock) {
++finalizeCount;
finalized[index] = true;
}
}
}
privatestaticvoid allocFinalizableObjects(int count) {
Object[] objs = new Object[count]; for (int i = 0; i < count; ++i) {
objs[i] = new FinalizeCounter(i);
}
}
privatestaticvoid runFinalizationTest() {
allocFinalizableObjects(FinalizeCounter.maxCount);
Runtime.getRuntime().gc();
System.runFinalization(); if (FinalizeCounter.getCount() != FinalizeCounter.maxCount) { if (isDalvik) { // runFinalization is "expend effort", only ART makes a strong effort all finalizers ran.
System.out.println("Finalized " + FinalizeCounter.getCount() + " / " + FinalizeCounter.maxCount); // Print out all the finalized elements.
FinalizeCounter.printNonFinalized();
} // Try to sleep for a couple seconds to see if the objects became finalized after. try {
java.lang.Thread.sleep(2000);
} catch (InterruptedException e) { thrownew AssertionError(e);
}
}
System.out.println("After sleep finalized " + FinalizeCounter.getCount() + " / " + FinalizeCounter.maxCount);
FinalizeCounter.printNonFinalized();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.