内存检测工具LeakCanary原理解析

一.配置

在build.gradle中可以进行配置:

//leakcanary
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    patchCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'

使用方法如下,需要在自定义的Application中使用

mRefWatcher = LeakCanary.install(this);
//以及
if (mRefWatcher != null) {
            mRefWatcher.watch(object);
}

二.源码分析

1、正常情况下一个Activity在destroy之后就要销毁,LeakCanary做的就是在一个Activity destroy之后将它放在一个WeakReference中,然后将这个WeakReference关联到一个ReferenceQueue,然后去检测这个ReferenceQueue是否存在这个Queue,不存在就证明这个Activity泄漏了(WeakReference和ReferenceQueue的特性可以百度下,用这种方法检测内存泄漏确实精巧)

源码如下:

public void watch(Object watchedReference, String referenceName) {
        if(this != DISABLED) {
            Preconditions.checkNotNull(watchedReference, "watchedReference");
            Preconditions.checkNotNull(referenceName, "referenceName");
            long watchStartNanoTime = System.nanoTime();
//分配独立的UUID
            String key = UUID.randomUUID().toString();
            this.retainedKeys.add(key);

//KeyedWeakReference 是WeakReference的子类
            KeyedWeakReference reference = new KeyedWeakReference(watchedReference, key, referenceName, this.queue);
            this.ensureGoneAsync(watchStartNanoTime, reference);
        }
    }

private void ensureGoneAsync(final long watchStartNanoTime, final KeyedWeakReference reference) {
        this.watchExecutor.execute(new Retryable() {
            public Result run() {
                return RefWatcher.this.ensureGone(reference, watchStartNanoTime);
            }
        });
    }

Result ensureGone(KeyedWeakReference reference, long watchStartNanoTime) {
        long gcStartNanoTime = System.nanoTime();
        long watchDurationMs = TimeUnit.NANOSECONDS.toMillis(gcStartNanoTime - watchStartNanoTime);
        this.removeWeaklyReachableReferences();
        if(this.debuggerControl.isDebuggerAttached()) {
            return Result.RETRY;
        } else if(this.gone(reference)) {
            return Result.DONE;
        } else {
//	这里会先要求一次Gc,然后再次判断
            this.gcTrigger.runGc();
            this.removeWeaklyReachableReferences();

            if(!this.gone(reference)) {
                long startDumpHeap = System.nanoTime();
                long gcDurationMs = TimeUnit.NANOSECONDS.toMillis(startDumpHeap - gcStartNanoTime);
                
//	导出dump文件并分析
		File heapDumpFile = this.heapDumper.dumpHeap();
                if(heapDumpFile == HeapDumper.RETRY_LATER) {
                    return Result.RETRY;
                }

                long heapDumpDurationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startDumpHeap);
                
//	将导出的dump文件序列话后,交与一个IntentService进行分析

		this.heapdumpListener.analyze(new HeapDump(heapDumpFile, reference.key, reference.name, this.excludedRefs, watchDurationMs, gcDurationMs, heapDumpDurationMs));
            }

            return Result.DONE;
        }
    }

详细的处理方法:

public AnalysisResult checkForLeak(File heapDumpFile, String referenceKey) {
        long analysisStartNanoTime = System.nanoTime();
        if(!heapDumpFile.exists()) {
            IllegalArgumentException e1 = new IllegalArgumentException("File does not exist: " + heapDumpFile);
            return AnalysisResult.failure(e1, this.since(analysisStartNanoTime));
        } else {
            try {
                MemoryMappedFileBuffer e = new MemoryMappedFileBuffer(heapDumpFile);
                HprofParser parser = new HprofParser(e);
                Snapshot snapshot = parser.parse();
                this.deduplicateGcRoots(snapshot);
                Instance leakingRef = this.findLeakingReference(referenceKey, snapshot);
                return leakingRef == null?AnalysisResult.noLeak(this.since(analysisStartNanoTime)):this.findLeakTrace(analysisStartNanoTime, snapshot, leakingRef);
            } catch (Throwable var9) {
                return AnalysisResult.failure(var9, this.since(analysisStartNanoTime));
            }
        }
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值