001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.testing;
018
019import static java.lang.Math.max;
020import static java.util.concurrent.TimeUnit.SECONDS;
021
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.annotations.J2ktIncompatible;
024import com.google.errorprone.annotations.DoNotMock;
025import com.google.j2objc.annotations.J2ObjCIncompatible;
026import java.lang.ref.WeakReference;
027import java.util.Locale;
028import java.util.concurrent.CancellationException;
029import java.util.concurrent.CountDownLatch;
030import java.util.concurrent.ExecutionException;
031import java.util.concurrent.Future;
032import java.util.concurrent.TimeoutException;
033
034/**
035 * Testing utilities relating to garbage collection finalization.
036 *
037 * <p>Use this class to test code triggered by <em>finalization</em>, that is, one of the following
038 * actions taken by the java garbage collection system:
039 *
040 * <ul>
041 *   <li>invoking the {@code finalize} methods of unreachable objects
042 *   <li>clearing weak references to unreachable referents
043 *   <li>enqueuing weak references to unreachable referents in their reference queue
044 * </ul>
045 *
046 * <p>This class uses (possibly repeated) invocations of {@link java.lang.System#gc()} to cause
047 * finalization to happen. However, a call to {@code System.gc()} is specified to be no more than a
048 * hint, so this technique may fail at the whim of the JDK implementation, for example if a user
049 * specified the JVM flag {@code -XX:+DisableExplicitGC}. But in practice, it works very well for
050 * ordinary tests.
051 *
052 * <p>Failure of the expected event to occur within an implementation-defined "reasonable" time
053 * period or an interrupt while waiting for the expected event will result in a {@link
054 * RuntimeException}.
055 *
056 * <p>Here's an example that tests a {@code finalize} method:
057 *
058 * <pre>{@code
059 * final CountDownLatch latch = new CountDownLatch(1);
060 * Object x = new MyClass() {
061 *   ...
062 *   protected void finalize() { latch.countDown(); ... }
063 * };
064 * x = null;  // Hint to the JIT that x is stack-unreachable
065 * GcFinalization.await(latch);
066 * }</pre>
067 *
068 * <p>Here's an example that uses a user-defined finalization predicate:
069 *
070 * <pre>{@code
071 * final WeakHashMap<Object, Object> map = new WeakHashMap<>();
072 * map.put(new Object(), Boolean.TRUE);
073 * GcFinalization.awaitDone(new FinalizationPredicate() {
074 *   public boolean isDone() {
075 *     return map.isEmpty();
076 *   }
077 * });
078 * }</pre>
079 *
080 * <p>Even if your non-test code does not use finalization, you can use this class to test for
081 * leaks, by ensuring that objects are no longer strongly referenced:
082 *
083 * <pre>{@code
084 * // Helper function keeps victim stack-unreachable.
085 * private WeakReference<Foo> fooWeakRef() {
086 *   Foo x = ....;
087 *   WeakReference<Foo> weakRef = new WeakReference<>(x);
088 *   // ... use x ...
089 *   x = null;  // Hint to the JIT that x is stack-unreachable
090 *   return weakRef;
091 * }
092 * public void testFooLeak() {
093 *   GcFinalization.awaitClear(fooWeakRef());
094 * }
095 * }</pre>
096 *
097 * <p>This class cannot currently be used to test soft references, since this class does not try to
098 * create the memory pressure required to cause soft references to be cleared.
099 *
100 * <p>This class only provides testing utilities. It is not designed for direct use in production or
101 * for benchmarking.
102 *
103 * @author mike nonemacher
104 * @author Martin Buchholz
105 * @since 11.0
106 */
107@GwtIncompatible
108@J2ktIncompatible
109@J2ObjCIncompatible // gc
110@ElementTypesAreNonnullByDefault
111public final class GcFinalization {
112  private GcFinalization() {}
113
114  /**
115   * 10 seconds ought to be long enough for any object to be GC'ed and finalized. Unless we have a
116   * gigantic heap, in which case we scale by heap size.
117   */
118  private static long timeoutSeconds() {
119    // This class can make no hard guarantees.  The methods in this class are inherently flaky, but
120    // we try hard to make them robust in practice.  We could additionally try to add in a system
121    // load timeout multiplier.  Or we could try to use a CPU time bound instead of wall clock time
122    // bound.  But these ideas are harder to implement.  We do not try to detect or handle a
123    // user-specified -XX:+DisableExplicitGC.
124    //
125    // TODO(user): Consider using
126    // java/lang/management/OperatingSystemMXBean.html#getSystemLoadAverage()
127    //
128    // TODO(user): Consider scaling by number of mutator threads,
129    // e.g. using Thread#activeCount()
130    return max(10L, Runtime.getRuntime().totalMemory() / (32L * 1024L * 1024L));
131  }
132
133  /**
134   * Waits until the given future {@linkplain Future#isDone is done}, invoking the garbage collector
135   * as necessary to try to ensure that this will happen.
136   *
137   * @throws RuntimeException if timed out or interrupted while waiting
138   */
139  @SuppressWarnings("removal") // b/260137033
140  public static void awaitDone(Future<?> future) {
141    if (future.isDone()) {
142      return;
143    }
144    long timeoutSeconds = timeoutSeconds();
145    long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
146    do {
147      System.runFinalization();
148      if (future.isDone()) {
149        return;
150      }
151      System.gc();
152      try {
153        future.get(1L, SECONDS);
154        return;
155      } catch (CancellationException | ExecutionException ok) {
156        return;
157      } catch (InterruptedException ie) {
158        throw new RuntimeException("Unexpected interrupt while waiting for future", ie);
159      } catch (TimeoutException tryHarder) {
160        /* OK */
161      }
162    } while (System.nanoTime() - deadline < 0);
163    throw formatRuntimeException("Future not done within %d second timeout", timeoutSeconds);
164  }
165
166  /**
167   * Waits until the given predicate returns true, invoking the garbage collector as necessary to
168   * try to ensure that this will happen.
169   *
170   * @throws RuntimeException if timed out or interrupted while waiting
171   */
172  @SuppressWarnings("removal") // b/260137033
173  public static void awaitDone(FinalizationPredicate predicate) {
174    if (predicate.isDone()) {
175      return;
176    }
177    long timeoutSeconds = timeoutSeconds();
178    long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
179    do {
180      System.runFinalization();
181      if (predicate.isDone()) {
182        return;
183      }
184      CountDownLatch done = new CountDownLatch(1);
185      createUnreachableLatchFinalizer(done);
186      await(done);
187      if (predicate.isDone()) {
188        return;
189      }
190    } while (System.nanoTime() - deadline < 0);
191    throw formatRuntimeException(
192        "Predicate did not become true within %d second timeout", timeoutSeconds);
193  }
194
195  /**
196   * Waits until the given latch has {@linkplain CountDownLatch#countDown counted down} to zero,
197   * invoking the garbage collector as necessary to try to ensure that this will happen.
198   *
199   * @throws RuntimeException if timed out or interrupted while waiting
200   */
201  @SuppressWarnings("removal") // b/260137033
202  public static void await(CountDownLatch latch) {
203    if (latch.getCount() == 0) {
204      return;
205    }
206    long timeoutSeconds = timeoutSeconds();
207    long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
208    do {
209      System.runFinalization();
210      if (latch.getCount() == 0) {
211        return;
212      }
213      System.gc();
214      try {
215        if (latch.await(1L, SECONDS)) {
216          return;
217        }
218      } catch (InterruptedException ie) {
219        throw new RuntimeException("Unexpected interrupt while waiting for latch", ie);
220      }
221    } while (System.nanoTime() - deadline < 0);
222    throw formatRuntimeException(
223        "Latch failed to count down within %d second timeout", timeoutSeconds);
224  }
225
226  /**
227   * Creates a garbage object that counts down the latch in its finalizer. Sequestered into a
228   * separate method to make it somewhat more likely to be unreachable.
229   */
230  private static void createUnreachableLatchFinalizer(CountDownLatch latch) {
231    Object unused =
232        new Object() {
233          @SuppressWarnings({"removal", "Finalize"}) // b/260137033
234          @Override
235          protected void finalize() {
236            latch.countDown();
237          }
238        };
239  }
240
241  /**
242   * A predicate that is expected to return true subsequent to <em>finalization</em>, that is, one
243   * of the following actions taken by the garbage collector when performing a full collection in
244   * response to {@link System#gc()}:
245   *
246   * <ul>
247   *   <li>invoking the {@code finalize} methods of unreachable objects
248   *   <li>clearing weak references to unreachable referents
249   *   <li>enqueuing weak references to unreachable referents in their reference queue
250   * </ul>
251   */
252  @DoNotMock("Implement with a lambda")
253  public interface FinalizationPredicate {
254    boolean isDone();
255  }
256
257  /**
258   * Waits until the given weak reference is cleared, invoking the garbage collector as necessary to
259   * try to ensure that this will happen.
260   *
261   * <p>This is a convenience method, equivalent to:
262   *
263   * <pre>{@code
264   * awaitDone(new FinalizationPredicate() {
265   *   public boolean isDone() {
266   *     return ref.get() == null;
267   *   }
268   * });
269   * }</pre>
270   *
271   * @throws RuntimeException if timed out or interrupted while waiting
272   */
273  public static void awaitClear(WeakReference<?> ref) {
274    awaitDone(
275        new FinalizationPredicate() {
276          @Override
277          public boolean isDone() {
278            return ref.get() == null;
279          }
280        });
281  }
282
283  /**
284   * Tries to perform a "full" garbage collection cycle (including processing of weak references and
285   * invocation of finalize methods) and waits for it to complete. Ensures that at least one weak
286   * reference has been cleared and one {@code finalize} method has been run before this method
287   * returns. This method may be useful when testing the garbage collection mechanism itself, or
288   * inhibiting a spontaneous GC initiation in subsequent code.
289   *
290   * <p>In contrast, a plain call to {@link java.lang.System#gc()} does not ensure finalization
291   * processing and may run concurrently, for example, if the JVM flag {@code
292   * -XX:+ExplicitGCInvokesConcurrent} is used.
293   *
294   * <p>Whenever possible, it is preferable to test directly for some observable change resulting
295   * from GC, as with {@link #awaitClear}. Because there are no guarantees for the order of GC
296   * finalization processing, there may still be some unfinished work for the GC to do after this
297   * method returns.
298   *
299   * <p>This method does not create any memory pressure as would be required to cause soft
300   * references to be processed.
301   *
302   * @throws RuntimeException if timed out or interrupted while waiting
303   * @since 12.0
304   */
305  @SuppressWarnings({"removal", "Finalize"}) // b/260137033
306  public static void awaitFullGc() {
307    CountDownLatch finalizerRan = new CountDownLatch(1);
308    WeakReference<Object> ref =
309        new WeakReference<>(
310            new Object() {
311              @Override
312              protected void finalize() {
313                finalizerRan.countDown();
314              }
315            });
316
317    await(finalizerRan);
318    awaitClear(ref);
319
320    // Hope to catch some stragglers queued up behind our finalizable object
321    System.runFinalization();
322  }
323
324  private static RuntimeException formatRuntimeException(String format, Object... args) {
325    return new RuntimeException(String.format(Locale.ROOT, format, args));
326  }
327}