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