Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8287671: Adjust ForceGC to invoke System::gc fewer times for negative…
… case

Reviewed-by: rriggs, bchristi, xuelei
  • Loading branch information
Mandy Chung committed Jun 6, 2022
1 parent c328f8f commit 2e332c2
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions test/lib/jdk/test/lib/util/ForceGC.java
Expand Up @@ -32,21 +32,24 @@
* 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;
}
}
Expand All @@ -68,12 +71,20 @@ public boolean await(BooleanSupplier s) {
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;
}
}

0 comments on commit 2e332c2

Please sign in to comment.