8287671: Adjust ForceGC to invoke System::gc fewer times for negative case

Reviewed-by: rriggs, bchristi, xuelei
This commit is contained in:
Mandy Chung 2022-06-06 21:11:37 +00:00
parent c328f8fa2a
commit 2e332c2760

View File

@ -32,21 +32,24 @@ import java.util.function.BooleanSupplier;
* Utility class to invoke System.gc()
*/
public class ForceGC {
private final CountDownLatch cleanerInvoked = new CountDownLatch(1);
private final Cleaner cleaner = Cleaner.create();
private final static Cleaner cleaner = Cleaner.create();
private final CountDownLatch cleanerInvoked;
private Object o;
private int gcCount = 0;
public ForceGC() {
this.o = new Object();
cleaner.register(o, () -> cleanerInvoked.countDown());
this.cleanerInvoked = new CountDownLatch(1);
cleaner.register(o, cleanerInvoked::countDown);
}
private void doit(int iter) {
try {
for (int i = 0; i < 10; i++) {
System.gc();
System.out.println("doit() iter: " + iter + ", gc " + i);
if (cleanerInvoked.await(1L, TimeUnit.SECONDS)) {
gcCount++;
if (cleanerInvoked.await(100L, TimeUnit.MILLISECONDS)) {
return;
}
}
@ -68,12 +71,20 @@ public class ForceGC {
o = null; // Keep reference to Object until now, to ensure the Cleaner
// doesn't count down the latch before await() is called.
for (int i = 0; i < 10; i++) {
if (s.getAsBoolean()) return true;
doit(i);
try { Thread.sleep(1000); } catch (InterruptedException e) {
if (s.getAsBoolean()) {
System.out.println("ForceGC condition met after System.gc() " + gcCount + " times");
return true;
}
doIt(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new AssertionError("unexpected interrupted sleep", e);
}
}
System.out.println("ForceGC condition not met after System.gc() " + gcCount + " times");
return false;
}
}