-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathGCUtils.java
More file actions
46 lines (40 loc) · 1.38 KB
/
GCUtils.java
File metadata and controls
46 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package datadog.trace.test.util;
import static java.util.concurrent.TimeUnit.MINUTES;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;
@SuppressFBWarnings("DM_GC")
public abstract class GCUtils {
public static void awaitGC() throws InterruptedException {
Object obj = new Object();
final WeakReference<Object> ref = new WeakReference<>(obj);
obj = null;
awaitGC(ref);
}
public static void awaitGC(final WeakReference<?> ref) throws InterruptedException {
awaitGC(ref, 1, MINUTES);
}
public static void awaitGC(final WeakReference<?> ref, final long duration, final TimeUnit unit)
throws InterruptedException {
System.gc();
System.runFinalization();
final long waitNanos = unit.toNanos(duration);
final long start = System.nanoTime();
while (System.nanoTime() - start < waitNanos) {
if (ref.get() == null) {
return;
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
System.gc();
System.runFinalization();
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for " + ref.get() + " to be GCed");
}
}
throw new RuntimeException("Timed out waiting for " + ref.get() + " to be GCed");
}
}