Skip to content

0x03e LeakMemory_en

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

LeakMemory

Extra dependencies

AndroidGodEye use LeakCanary to analyze memory leak, you need to add dependency as following

implementation 'cn.hikyson.godeye:godeye-leakcanary:VERSION_NAME'

If you already have LeakCanary in your project, it may has come conflicts. Please delete this dependency and send leak memory info to Leak module of AndroidGodEye like this:

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());

Install

Use the following configuration to install

GodEye.instance().install(GodEyeConfig.defaultConfigBuilder().withLeakConfig(new GodEyeConfig.LeakConfig()).build());

or

<leakCanary />

Production and consumption of data

Use the following methods to observe the output:

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 will be output after analyzd leak info.

DebugMonitor Dashboard

android_god_eye_leak

Clone this wiki locally