/** * Creates a {@link RefWatcher} that works out of the box, and starts watching activity * references (on ICS+). */ public static @NonNull RefWatcher install(@NonNull Application application) { return refWatcher(application).listenerServiceClass(DisplayLeakService.class) .excludedRefs(AndroidExcludedRefs.createAppDefaults().build()) .buildAndInstall(); }
/** * Sets a custom delay for how long the {@link RefWatcher} should wait until it checks if a * tracked object has been garbage collected. This overrides any call to {@link * #watchExecutor(WatchExecutor)}. */ public @NonNull AndroidRefWatcherBuilder watchDelay(long delay, @NonNull TimeUnit unit) { return watchExecutor(new AndroidWatchExecutor(unit.toMillis(delay))); }
public static @NonNull AndroidRefWatcherBuilder refWatcher(@NonNull Context context) { return new AndroidRefWatcherBuilder(context); }
@Override protected void setupLeakCanary() { InstrumentationLeakDetector.instrumentationRefWatcher(this) .buildAndInstall(); } }
/** * Sets a custom {@link AbstractAnalysisResultService} to listen to analysis results. This * overrides any call to {@link #heapDumpListener(HeapDump.Listener)}. */ public @NonNull AndroidRefWatcherBuilder listenerServiceClass( @NonNull Class<? extends AbstractAnalysisResultService> listenerServiceClass) { enableDisplayLeakActivity = DisplayLeakService.class.isAssignableFrom(listenerServiceClass); return heapDumpListener(new ServiceHeapDumpListener(context, listenerServiceClass)); }
/** * Creates a {@link RefWatcher} instance and makes it available through {@link * LeakCanary#installedRefWatcher()}. * * Also starts watching activity references if {@link #watchActivities(boolean)} was set to true. * * @throws UnsupportedOperationException if called more than once per Android process. */ public @NonNull RefWatcher buildAndInstall() { if (LeakCanaryInternals.installedRefWatcher != null) { throw new UnsupportedOperationException("buildAndInstall() should only be called once."); } RefWatcher refWatcher = build(); if (refWatcher != DISABLED) { if (enableDisplayLeakActivity) { LeakCanaryInternals.setEnabledAsync(context, DisplayLeakActivity.class, true); } if (watchActivities) { ActivityRefWatcher.install(context, refWatcher); } if (watchFragments) { FragmentRefWatcher.Helper.install(context, refWatcher); } } LeakCanaryInternals.installedRefWatcher = refWatcher; return refWatcher; }
@Override public void onCreate() { super.onCreate(); InstrumentationLeakDetector.instrumentationRefWatcher(this) .buildAndInstall(); } }
@Override public void onCreate() { super.onCreate(); InstrumentationLeakDetector.instrumentationRefWatcher(this) .buildAndInstall(); } }
/** * Returns a new {@link} AndroidRefWatcherBuilder that will create a {@link RefWatcher} suitable * for instrumentation tests. This {@link RefWatcher} will never trigger a heap dump. This should * be installed from the test application class, and should be used in combination with a * {@link RunListener} that calls {@link #detectLeaks()}, for instance * {@link FailTestOnLeakRunListener}. */ public static @NonNull AndroidRefWatcherBuilder instrumentationRefWatcher( @NonNull Application application) { return LeakCanary.refWatcher(application) .watchExecutor(new WatchExecutor() { // Storing weak refs to ensure they make it to the queue. final List<Retryable> trackedReferences = new CopyOnWriteArrayList<>(); @Override public void execute(Retryable retryable) { trackedReferences.add(retryable); } }); }