细节点 — 1
LeakCanary默认只会分析Activity的内存泄漏。那可不可以用来检测其他的对象呢?
答案是肯定的。
LeakCanary.install()
调用之后,会返回RefWatcher
对象,然后直接调用watch()
方法即可。
所以可以这么写:
private static RefWatcher sRefWatcher;
public static RefWatcher getRefWatcher() {
return sRefWatcher;
}
protected void setupLeakCanary() {
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
sRefWatcher = LeakCanary.install(this);
}
然后,我们在确定某个object已经不再需要的时候,去调用watch
,通过这种方式我们就可以对任何对象都进行检测了。
sRefWatcher.watch(object)