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