-
Notifications
You must be signed in to change notification settings - Fork 344
0x03e LeakMemory_zh
hui.zhao edited this page Mar 27, 2020
·
5 revisions
AndroidGodEye依赖LeakCanary来提供内存泄漏分析服务,请添加如下依赖
implementation 'cn.hikyson.godeye:godeye-leakcanary:VERSION_NAME'
如果你的项目中已经有LeakCanary依赖,那么不要依赖这个库,并且在发生内存泄漏的时候给AndroidGodEye的Leak模块发送数据:
LeakCanary.setConfig(new LeakCanary.Config().newBuilder()
.onHeapAnalyzedListener(new OnHeapAnalyzedListener() {
@Override
public void onHeapAnalyzed(@NotNull HeapAnalysis heapAnalysis) {
if (heapAnalysis instanceof HeapAnalysisFailure) {
return;
}
if (!(heapAnalysis instanceof HeapAnalysisSuccess)) {
return;
}
final HeapAnalysisSuccess analysisSuccess = (HeapAnalysisSuccess) heapAnalysis;
IteratorUtil.forEach(analysisSuccess.getAllLeaks().iterator(), new Consumer<shark.Leak>() {
@Override
public void accept(shark.Leak leak) {
GodEye.instance().<Leak>getModule(GodEye.ModuleName.LEAK_CANARY).produce(new LeakInfo(analysisSuccess.getCreatedAtTimeMillis(), analysisSuccess.getAnalysisDurationMillis(), leak));
}
});
}
}).build());
使用如下配置进行安装
GodEye.instance().install(GodEyeConfig.defaultConfigBuilder().withLeakConfig(new GodEyeConfig.LeakConfig()).build());
或者
<leakCanary />
使用如下方式监听模块输出的数据:
try {
GodEye.instance().observeModule(GodEye.ModuleName.LEAK_CANARY, new Consumer<LeakInfo>() {
@Override
public void accept(LeakInfo leakInfo) throws Exception {
}
});
} catch (UninstallException e) {
e.printStackTrace();
}
leakInfo
会在泄漏信息分析完成之后进行输出