private void initializeLeakCanary() { if (LeakCanary.isInAnalyzerProcess(getApplicationContext())) { // This process is dedicated to LeakCanary for heap analysis. // You should not init your app in this process. mRefWatcher = RefWatcher.DISABLED; } else { // Ignore known Android SDK leaks ExcludedRefs excludedRefs = AndroidExcludedRefs.createAppDefaults() .build(); mRefWatcher = LeakCanary.refWatcher(getApplicationContext()) .excludedRefs(excludedRefs) .buildAndInstall(); } }
public static RefWatcher install(Application application) { if (BuildConfig.DEBUG) { ExcludedRefs excludedRefs = AndroidExcludedRefs.createAppDefaults() //exclude InputMethodManager .clazz("android.view.inputmethod.InputMethodManager") .build(); return LeakCanary.refWatcher(application) .listenerServiceClass(DisplayLeakService.class) .excludedRefs(excludedRefs) .buildAndInstall(); } else { return LeakCanary.install(application); } }
@Override protected void onHandleIntent(Intent intent) { String listenerClassName = intent.getStringExtra(LISTENER_CLASS_EXTRA); HeapDump heapDump = (HeapDump) intent.getSerializableExtra(HEAPDUMP_EXTRA); ExcludedRefs androidExcludedDefault = createAndroidDefaults().build(); HeapAnalyzer heapAnalyzer = new HeapAnalyzer(androidExcludedDefault, heapDump.excludedRefs); AnalysisResult result = heapAnalyzer.checkForLeak(heapDump.heapDumpFile, heapDump.referenceKey); AbstractAnalysisResultService.sendResultToListener(this, listenerClassName, heapDump, result); }