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 com.google.common.base.Throwables.throwIfUnchecked;
020import static com.google.common.collect.Sets.newConcurrentHashSet;
021import static java.lang.Math.max;
022import static java.util.concurrent.TimeUnit.SECONDS;
023
024import com.google.common.annotations.GwtIncompatible;
025import com.google.common.annotations.J2ktIncompatible;
026import com.google.common.base.FinalizablePhantomReference;
027import com.google.common.base.FinalizableReferenceQueue;
028import com.google.errorprone.annotations.DoNotMock;
029import com.google.errorprone.annotations.FormatMethod;
030import com.google.j2objc.annotations.J2ObjCIncompatible;
031import java.lang.ref.WeakReference;
032import java.lang.reflect.InvocationTargetException;
033import java.lang.reflect.Method;
034import java.util.Locale;
035import java.util.Set;
036import java.util.concurrent.CancellationException;
037import java.util.concurrent.CountDownLatch;
038import java.util.concurrent.ExecutionException;
039import java.util.concurrent.Future;
040import java.util.concurrent.TimeoutException;
041import org.jspecify.annotations.NullMarked;
042import org.jspecify.annotations.Nullable;
043
044/**
045 * Testing utilities relating to garbage collection finalization.
046 *
047 * <p>Use this class to test code triggered by <em>finalization</em>, that is, one of the following
048 * actions taken by the java garbage collection system:
049 *
050 * <ul>
051 *   <li>invoking the {@code finalize} methods of unreachable objects
052 *   <li>clearing weak references to unreachable referents
053 *   <li>enqueuing weak references to unreachable referents in their reference queue
054 * </ul>
055 *
056 * <p>This class uses (possibly repeated) invocations of {@link java.lang.System#gc()} to cause
057 * finalization to happen. However, a call to {@code System.gc()} is specified to be no more than a
058 * hint, so this technique may fail at the whim of the JDK implementation, for example if a user
059 * specified the JVM flag {@code -XX:+DisableExplicitGC}. But in practice, it works very well for
060 * ordinary tests.
061 *
062 * <p>Failure of the expected event to occur within an implementation-defined "reasonable" time
063 * period or an interrupt while waiting for the expected event will result in a {@link
064 * RuntimeException}.
065 *
066 * <p>Here's an example that tests a {@code finalize} method:
067 *
068 * {@snippet :
069 * final CountDownLatch latch = new CountDownLatch(1);
070 * Object x = new MyClass() {
071 *   ...
072 *   protected void finalize() { latch.countDown(); ... }
073 * };
074 * x = null; // Hint to the JIT that x is stack-unreachable
075 * GcFinalization.await(latch);
076 * }
077 *
078 * <p>Here's an example that uses a user-defined finalization predicate:
079 *
080 * {@snippet :
081 * final WeakHashMap<Object, Object> map = new WeakHashMap<>();
082 * map.put(new Object(), Boolean.TRUE);
083 * GcFinalization.awaitDone(new FinalizationPredicate() {
084 *   public boolean isDone() {
085 *     return map.isEmpty();
086 *   }
087 * });
088 * }
089 *
090 * <p>Even if your non-test code does not use finalization, you can use this class to test for
091 * leaks, by ensuring that objects are no longer strongly referenced:
092 *
093 * {@snippet :
094 * // Helper function keeps victim stack-unreachable.
095 * private WeakReference<Foo> fooWeakRef() {
096 *   Foo x = ....;
097 *   WeakReference<Foo> weakRef = new WeakReference<>(x);
098 *   // ... use x ...
099 *   x = null; // Hint to the JIT that x is stack-unreachable
100 *   return weakRef;
101 * }
102 * public void testFooLeak() {
103 *   GcFinalization.awaitClear(fooWeakRef());
104 * }
105 * }
106 *
107 * <p>This class cannot currently be used to test soft references, since this class does not try to
108 * create the memory pressure required to cause soft references to be cleared.
109 *
110 * <p>This class only provides testing utilities. It is not designed for direct use in production or
111 * for benchmarking.
112 *
113 * @author mike nonemacher
114 * @author Martin Buchholz
115 * @since 11.0
116 */
117@GwtIncompatible
118@J2ktIncompatible
119@J2ObjCIncompatible // gc
120@NullMarked
121public final class GcFinalization {
122  private GcFinalization() {}
123
124  /**
125   * 10 seconds ought to be long enough for any object to be GC'ed and finalized. Unless we have a
126   * gigantic heap, in which case we scale by heap size.
127   */
128  private static long timeoutSeconds() {
129    // This class can make no hard guarantees.  The methods in this class are inherently flaky, but
130    // we try hard to make them robust in practice.  We could additionally try to add in a system
131    // load timeout multiplier.  Or we could try to use a CPU time bound instead of wall clock time
132    // bound.  But these ideas are harder to implement.  We do not try to detect or handle a
133    // user-specified -XX:+DisableExplicitGC.
134    //
135    // TODO(user): Consider using
136    // java/lang/management/OperatingSystemMXBean.html#getSystemLoadAverage()
137    //
138    // TODO(user): Consider scaling by number of mutator threads,
139    // e.g. using Thread#activeCount()
140    return max(10L, Runtime.getRuntime().totalMemory() / (32L * 1024L * 1024L));
141  }
142
143  /**
144   * Waits until the given future {@linkplain Future#isDone is done}, invoking the garbage collector
145   * as necessary to try to ensure that this will happen.
146   *
147   * @throws RuntimeException if timed out or interrupted while waiting
148   */
149  public static void awaitDone(Future<?> future) {
150    if (future.isDone()) {
151      return;
152    }
153    long timeoutSeconds = timeoutSeconds();
154    long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
155    do {
156      runFinalization();
157      if (future.isDone()) {
158        return;
159      }
160      System.gc();
161      try {
162        future.get(1L, SECONDS);
163        return;
164      } catch (CancellationException | ExecutionException ok) {
165        return;
166      } catch (InterruptedException ie) {
167        throw new RuntimeException("Unexpected interrupt while waiting for future", ie);
168      } catch (TimeoutException tryHarder) {
169        /* OK */
170      }
171    } while (System.nanoTime() - deadline < 0);
172    throw formatRuntimeException("Future not done within %d second timeout", timeoutSeconds);
173  }
174
175  /**
176   * Waits until the given predicate returns true, invoking the garbage collector as necessary to
177   * try to ensure that this will happen.
178   *
179   * @throws RuntimeException if timed out or interrupted while waiting
180   */
181  public static void awaitDone(FinalizationPredicate predicate) {
182    if (predicate.isDone()) {
183      return;
184    }
185    long timeoutSeconds = timeoutSeconds();
186    long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
187    do {
188      runFinalization();
189      if (predicate.isDone()) {
190        return;
191      }
192      CountDownLatch done = new CountDownLatch(1);
193      createUnreachableLatchFinalizer(done);
194      await(done);
195      if (predicate.isDone()) {
196        return;
197      }
198    } while (System.nanoTime() - deadline < 0);
199    throw formatRuntimeException(
200        "Predicate did not become true within %d second timeout", timeoutSeconds);
201  }
202
203  /**
204   * Waits until the given latch has {@linkplain CountDownLatch#countDown counted down} to zero,
205   * invoking the garbage collector as necessary to try to ensure that this will happen.
206   *
207   * @throws RuntimeException if timed out or interrupted while waiting
208   */
209  public static void await(CountDownLatch latch) {
210    if (latch.getCount() == 0) {
211      return;
212    }
213    long timeoutSeconds = timeoutSeconds();
214    long deadline = System.nanoTime() + SECONDS.toNanos(timeoutSeconds);
215    do {
216      runFinalization();
217      if (latch.getCount() == 0) {
218        return;
219      }
220      System.gc();
221      try {
222        if (latch.await(1L, SECONDS)) {
223          return;
224        }
225      } catch (InterruptedException ie) {
226        throw new RuntimeException("Unexpected interrupt while waiting for latch", ie);
227      }
228    } while (System.nanoTime() - deadline < 0);
229    throw formatRuntimeException(
230        "Latch failed to count down within %d second timeout", timeoutSeconds);
231  }
232
233  /**
234   * Creates a garbage object that counts down the latch in its finalizer. Sequestered into a
235   * separate method to make it somewhat more likely to be unreachable.
236   */
237  private static void createUnreachableLatchFinalizer(CountDownLatch latch) {
238    FinalizableReference.register(new Object(), latch);
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    /** Returns {@code true} if the finalization condition is met. */
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  public static void awaitFullGc() {
301    CountDownLatch finalizerRan = new CountDownLatch(1);
302    WeakReference<Object> ref = createWeakReferenceWithFinalizer(finalizerRan);
303
304    await(finalizerRan);
305    awaitClear(ref);
306
307    // Hope to catch some stragglers queued up behind our finalizable object
308    runFinalization();
309  }
310
311  private static WeakReference<Object> createWeakReferenceWithFinalizer(
312      CountDownLatch finalizerRan) {
313    Object referent = new Object();
314    FinalizableReference.register(referent, finalizerRan);
315    return new WeakReference<>(referent);
316  }
317
318  private static final class FinalizableReference extends FinalizablePhantomReference<Object> {
319    static final FinalizableReferenceQueue referenceQueue = new FinalizableReferenceQueue();
320    static final Set<FinalizableReference> references = newConcurrentHashSet();
321
322    static void register(Object referent, CountDownLatch latch) {
323      references.add(new FinalizableReference(referent, latch));
324    }
325
326    final CountDownLatch latch;
327
328    FinalizableReference(Object referent, CountDownLatch latch) {
329      super(referent, referenceQueue);
330      this.latch = latch;
331    }
332
333    @Override
334    public void finalizeReferent() {
335      references.remove(this);
336      latch.countDown();
337    }
338  }
339
340  @FormatMethod
341  private static RuntimeException formatRuntimeException(String format, Object... args) {
342    return new RuntimeException(String.format(Locale.ROOT, format, args));
343  }
344
345  private static final @Nullable Method runFinalizationMethod = getRunFinalizationMethod();
346
347  private static @Nullable Method getRunFinalizationMethod() {
348    try {
349      return System.class.getMethod("runFinalization");
350    } catch (NoSuchMethodException | SecurityException e) {
351      return null;
352    }
353  }
354
355  private static void runFinalization() {
356    if (runFinalizationMethod == null) {
357      return;
358    }
359    try {
360      runFinalizationMethod.invoke(null);
361    } catch (InvocationTargetException e) {
362      throwIfUnchecked(e.getCause());
363      throw new AssertionError(e.getCause());
364    } catch (IllegalAccessException e) {
365      throw new AssertionError(e);
366    }
367  }
368}