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); //监听Activity·生命周期
}
if (watchFragments) {
FragmentRefWatcher.Helper.install(context, refWatcher);//监听Fragment的生命周期
}
}
LeakCanaryInternals.installedRefWatcher = refWatcher;
return refWatcher;
}
public static void install(@NonNull Context context, @NonNull RefWatcher refWatcher) {
Application application = (Application) context.getApplicationContext();
ActivityRefWatcher activityRefWatcher = new ActivityRefWatcher(application, refWatcher);
application.registerActivityLifecycleCallbacks(activityRefWatcher.lifecycleCallbacks);
}
private final Application.ActivityLifecycleCallbacks lifecycleCallbacks =
new ActivityLifecycleCallbacksAdapter() {
@Override public void onActivityDestroyed(Activity activity) {
refWatcher.watch(activity);
}
};
private final WatchExecutor watchExecutor;
private final DebuggerControl debuggerControl;//调试控制
private final GcTrigger gcTrigger;//处理GC的
private final HeapDumper heapDumper;//栈堆
private final HeapDump.Listener heapdumpListener;//
private final HeapDump.Builder heapDumpBuilder;
private final Set<String> retainedKeys;//有内存泄露的key
private final ReferenceQueue<Object> queue;//引用队列
好了 我们看到watch方法
public void watch(Object watchedReference) {
watch(watchedReference, "");
}
public void watch(Object watchedReference, String referenceName) {
if (this == DISABLED) {
return;
}
checkNotNull(watchedReference, "watchedReference");
checkNotNull(referenceName, "referenceName");
final long watchStartNanoTime = System.nanoTime(); //
String key = UUID.randomUUID().toString();//先获取对象唯一的key值
retainedKeys.add(key);//添加到集合之中
final KeyedWeakReference reference =
new KeyedWeakReference(watchedReference, key, referenceName, queue);//创建一个弱引用
ensureGoneAsync(watchStartNanoTime, reference);//确认这个弱引用是否被回收了
}
private void ensureGoneAsync(final long watchStartNanoTime, final KeyedWeakReference reference) {
watchExecutor.execute(new Retryable() {//在线程池中去执行ensureGone方法
@Override public Retryable.Result run() {
return ensureGone(reference, watchStartNanoTime);
}
});
}
其实最后是到了 ensureGone 这个核心方法中
Retryable.Result ensureGone(final KeyedWeakReference reference, final long watchStartNanoTime) {
removeWeaklyReachableReferences();
if (debuggerControl.isDebuggerAttached()) {//如果是debug状态那么久不会内存管理
// The debugger can create false leaks.
return RETRY;
}
if (gone(reference)) {//如果可达 那么不属于内存泄露
return DONE;
}
gcTrigger.runGc();//先手动gc一波
removeWeaklyReachableReferences(); //
if (!go
ne(reference)) {//如果还存在内存泄露
long startDumpHeap = System.nanoTime();//记录时间
long gcDurationMs = NANOSECONDS.toMillis(startDumpHeap - gcStartNanoTime);
File heapDumpFile = heapDumper.dumpHeap();
if (heapDumpFile == RETRY_LATER) {
// Could not dump the heap.
return RETRY;
}
long heapDumpDurationMs = NANOSECONDS.toMillis(System.nanoTime() - startDumpHeap);
HeapDump heapDump = heapDumpBuilder.heapDumpFile(heapDumpFile).referenceKey(reference.key)
.referenceName(reference.name)
.watchDurationMs(watchDurationMs)
.gcDurationMs(gcDurationMs)
.heapDumpDurationMs(heapDumpDurationMs)
.build();//生产出一个heapDump 对象
heapdumpListener.analyze(heapDump); 分析原因
}
return DONE;
}