34 |
|
|
35 |
|
import jdk.test.lib.RandomFactory; |
36 |
|
|
37 |
+ |
import java.lang.ref.ReferenceQueue; |
38 |
|
import java.lang.ref.WeakReference; |
39 |
|
import java.util.Arrays; |
40 |
|
import java.util.Iterator; |
45 |
|
import java.util.concurrent.CountDownLatch; |
46 |
|
import java.util.function.BooleanSupplier; |
47 |
|
|
48 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
48 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
49 |
|
|
50 |
|
public class GCDuringIteration { |
51 |
|
|
52 |
|
/** No guarantees, but effective in practice. */ |
53 |
|
static void forceFullGc() { |
54 |
+ |
long timeoutMillis = 1000L; |
55 |
|
CountDownLatch finalizeDone = new CountDownLatch(1); |
56 |
< |
WeakReference<?> ref = new WeakReference<Object>(new Object() { |
57 |
< |
protected void finalize() { finalizeDone.countDown(); }}); |
56 |
> |
ReferenceQueue<Object> queue = new ReferenceQueue<>(); |
57 |
> |
WeakReference<Object> ref = new WeakReference<>( |
58 |
> |
new Object() { |
59 |
> |
protected void finalize() { finalizeDone.countDown(); }}, |
60 |
> |
queue); |
61 |
|
try { |
62 |
< |
for (int i = 0; i < 10; i++) { |
62 |
> |
for (int tries = 3; tries--> 0; ) { |
63 |
|
System.gc(); |
64 |
< |
if (finalizeDone.await(1L, SECONDS) && ref.get() == null) { |
64 |
> |
if (finalizeDone.await(timeoutMillis, MILLISECONDS) |
65 |
> |
&& queue.remove(timeoutMillis) != null |
66 |
> |
&& ref.get() == null) { |
67 |
|
System.runFinalization(); // try to pick up stragglers |
68 |
|
return; |
69 |
|
} |
70 |
+ |
timeoutMillis *= 4; |
71 |
|
} |
72 |
|
} catch (InterruptedException unexpected) { |
73 |
|
throw new AssertionError("unexpected InterruptedException"); |