Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8287671: Adjust ForceGC to invoke System::gc fewer times for negative case #9021

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions test/lib/jdk/test/lib/util/ForceGC.java
Original file line number Diff line number Diff line change
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;
}
}