Skip to content

0x03e LeakMemory_zh

hui.zhao edited this page Mar 27, 2020 · 5 revisions

LeakMemory

额外依赖

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会在泄漏信息分析完成之后进行输出

DebugMonitor Dashboard

android_god_eye_leak

Clone this wiki locally