@GwtIncompatible // weak references public void testFreesNextReference() { Iterator<Object> itr = new AbstractIterator<Object>() { @Override public Object computeNext() { return new Object(); } }; WeakReference<Object> ref = new WeakReference<>(itr.next()); GcFinalization.awaitClear(ref); }
/** * Waits until the given weak reference is cleared, invoking the garbage collector as necessary to * try to ensure that this will happen. * * <p>This is a convenience method, equivalent to: * * <pre>{@code * awaitDone(new FinalizationPredicate() { * public boolean isDone() { * return ref.get() == null; * } * }); * }</pre> * * @throws RuntimeException if timed out or interrupted while waiting */ public static void awaitClear(final WeakReference<?> ref) { awaitDone( new FinalizationPredicate() { public boolean isDone() { return ref.get() == null; } }); }
await(finalizerRan); awaitClear(ref);
/** * Waits until the given latch has {@linkplain CountDownLatch#countDown counted down} to zero, * invoking the garbage collector as necessary to try to ensure that this will happen. * * @throws RuntimeException if timed out or interrupted while waiting */ public static void await(CountDownLatch latch) { if (latch.getCount() == 0) { return; } final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { System.runFinalization(); if (latch.getCount() == 0) { return; } System.gc(); try { if (latch.await(1L, SECONDS)) { return; } } catch (InterruptedException ie) { throw new RuntimeException("Unexpected interrupt while waiting for latch", ie); } } while (System.nanoTime() - deadline < 0); throw formatRuntimeException( "Latch failed to count down within %d second timeout", timeoutSeconds); }
/** * Waits until the given predicate returns true, invoking the garbage collector as necessary to * try to ensure that this will happen. * * @throws RuntimeException if timed out or interrupted while waiting */ public static void awaitDone(FinalizationPredicate predicate) { if (predicate.isDone()) { return; } final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { System.runFinalization(); if (predicate.isDone()) { return; } CountDownLatch done = new CountDownLatch(1); createUnreachableLatchFinalizer(done); await(done); if (predicate.isDone()) { return; } } while (System.nanoTime() - deadline < 0); throw formatRuntimeException( "Predicate did not become true within %d second timeout", timeoutSeconds); }
/** * awaitFullGc() is not quite as reliable a way to ensure calling of a specific finalize method as * the more direct await* methods, but should be reliable enough in practice to avoid flakiness of * this test. (And if it isn't, we'd like to know about it first!) */ public void testAwaitFullGc() { final CountDownLatch finalizerRan = new CountDownLatch(1); final WeakReference<Object> ref = new WeakReference<Object>( new Object() { @Override protected void finalize() { finalizerRan.countDown(); } }); // Don't copy this into your own test! // Use e.g. awaitClear or await(CountDownLatch) instead. GcFinalization.awaitFullGc(); // If this test turns out to be flaky, add a second call to awaitFullGc() // GcFinalization.awaitFullGc(); assertEquals(0, finalizerRan.getCount()); assertNull(ref.get()); } }
/** * Waits until the given predicate returns true, invoking the garbage collector as necessary to * try to ensure that this will happen. * * @throws RuntimeException if timed out or interrupted while waiting */ public static void awaitDone(FinalizationPredicate predicate) { if (predicate.isDone()) { return; } final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { System.runFinalization(); if (predicate.isDone()) { return; } CountDownLatch done = new CountDownLatch(1); createUnreachableLatchFinalizer(done); await(done); if (predicate.isDone()) { return; } } while (System.nanoTime() - deadline < 0); throw new RuntimeException( String.format("Predicate did not become true within %d second timeout", timeoutSeconds)); }
public void testAwait_CountDownLatch() { final CountDownLatch latch = new CountDownLatch(1); Object x = new Object() { @Override protected void finalize() { latch.countDown(); } }; x = null; // Hint to the JIT that x is unreachable GcFinalization.await(latch); assertEquals(0, latch.getCount()); }
/** * Waits until the given latch has {@linkplain CountDownLatch#countDown counted down} to zero, * invoking the garbage collector as necessary to try to ensure that this will happen. * * @throws RuntimeException if timed out or interrupted while waiting */ public static void await(CountDownLatch latch) { if (latch.getCount() == 0) { return; } final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { System.runFinalization(); if (latch.getCount() == 0) { return; } System.gc(); try { if (latch.await(1L, SECONDS)) { return; } } catch (InterruptedException ie) { throw new RuntimeException("Unexpected interrupt while waiting for latch", ie); } } while (System.nanoTime() - deadline < 0); throw new RuntimeException( String.format("Latch failed to count down within %d second timeout", timeoutSeconds)); }
@Test(dataProvider = "caches") @CacheSpec(requiresWeakOrSoft = true) public void iterators(Map<Integer, Integer> map, CacheContext context) { context.clear(); GcFinalization.awaitFullGc(); assertThat(Iterators.size(map.keySet().iterator()), is(0)); assertThat(Iterators.size(map.values().iterator()), is(0)); assertThat(Iterators.size(map.entrySet().iterator()), is(0)); }
/** * Waits until the given predicate returns true, invoking the garbage collector as necessary to * try to ensure that this will happen. * * @throws RuntimeException if timed out or interrupted while waiting */ public static void awaitDone(FinalizationPredicate predicate) { if (predicate.isDone()) { return; } final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { System.runFinalization(); if (predicate.isDone()) { return; } CountDownLatch done = new CountDownLatch(1); createUnreachableLatchFinalizer(done); await(done); if (predicate.isDone()) { return; } } while (System.nanoTime() - deadline < 0); throw formatRuntimeException( "Predicate did not become true within %d second timeout", timeoutSeconds); }
public void testAwait_CountDownLatch_Interrupted() { Interruptenator interruptenator = new Interruptenator(Thread.currentThread()); try { final CountDownLatch latch = new CountDownLatch(1); try { GcFinalization.await(latch); fail("should throw"); } catch (RuntimeException expected) { assertWrapsInterruptedException(expected); } } finally { interruptenator.shutdown(); Thread.interrupted(); } }
return; final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { throw formatRuntimeException("Future not done within %d second timeout", timeoutSeconds);
return; final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do {
public void testGetChecked_classUnloading() throws Exception { WeakReference<?> classUsedByGetChecked = doTestClassUnloading(); GcFinalization.awaitClear(classUsedByGetChecked); }
public void testAwaitDone_FinalizationPredicate() { final WeakHashMap<Object, Object> map = new WeakHashMap<>(); map.put(new Object(), Boolean.TRUE); GcFinalization.awaitDone( new FinalizationPredicate() { public boolean isDone() { return map.isEmpty(); } }); assertTrue(map.isEmpty()); }
@Test(dataProvider = "caches") @CacheSpec(requiresWeakOrSoft = true, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT, population = Population.FULL, stats = Stats.ENABLED, removalListener = Listener.CONSUMING) public void isEmpty(Map<Integer, Integer> map, CacheContext context) { context.clear(); GcFinalization.awaitFullGc(); assertThat(map.isEmpty(), is(false)); awaitFullCleanup(context.cache()); assertThat(map.isEmpty(), is(true)); }
}); await(finalizerRan); awaitClear(ref);
public void testAwait_CountDownLatch() { final CountDownLatch latch = new CountDownLatch(1); Object x = new Object() { @Override protected void finalize() { latch.countDown(); } }; x = null; // Hint to the JIT that x is unreachable GcFinalization.await(latch); assertEquals(0, latch.getCount()); }
return; final long timeoutSeconds = timeoutSeconds(); final long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds); do { throw formatRuntimeException("Future not done within %d second timeout", timeoutSeconds);