Skip to content

0x03e LeakMemory_zh

hui.zhao edited this page Dec 7, 2019 · 5 revisions

LeakMemory

安装

使用如下配置进行安装

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

或者

<leakMemory debug="true" debugNotification="true" leakRefInfoProvider="cn.hikyson.godeye.core.internal.modules.leakdetector.DefaultLeakRefInfoProvider"/>

配置的leakRefInfoProvider用于提供泄漏的页面信息,默认使用AndroidGodEye中的DefaultLeakRefInfoProvider,可以自定义leakRefInfoProvider,实现LeakRefInfoProvider接口即可,这里提供的信息会带入后续的回调leakMemoryInfo中,debug表示是否需要在debug环境下使用,如果是true,那么会分析泄漏的所有信息,如果是false,那么只会获取页面是否泄漏,没有详细泄漏信息,而debugNotification表示是否需要在泄漏的时候在手机上展示泄漏的通知

数据生产与消费

使用如下方式监听模块输出的数据:

try {
            GodEye.instance().observeModule(GodEye.ModuleName.LEAK, new Consumer<LeakQueue.LeakMemoryInfo>() {
                @Override
                public void accept(LeakQueue.LeakMemoryInfo leakMemoryInfo) throws Exception {
                }
            });
        } catch (UninstallException e) {
            e.printStackTrace();
        }

leakMemoryInfo会在检测到泄漏、处理分析泄漏、和分析完成之后进行输出,检测过程分成多个步骤,leakMemoryInfo.status保存了当前泄露检测的进度,进度的状态如下,只有STATUS_DONE才是检测完全结束了:

@Retention(RetentionPolicy.SOURCE)
        @IntDef({Status.STATUS_INVALID, Status.STATUS_DETECT, Status.STATUS_START, Status.STATUS_PROGRESS, Status.STATUS_RETRY, Status.STATUS_DONE})
        public @interface Status {
            public static final int STATUS_INVALID = -1;
            public static final int STATUS_DETECT = 0;
            public static final int STATUS_START = 1;
            public static final int STATUS_PROGRESS = 2;
            public static final int STATUS_RETRY = 3;
            public static final int STATUS_DONE = 4;
        }

由于分析内存泄漏非常消耗cpu和内存,所以在生产环境下使用需要设置配置中的debug和debugNotification为false,这样生产环境的步骤只会有STATUS_DONE,而且回调中的leakMemoryInfo也不会有分析的gc root等信息

DebugMonitor Dashboard

android_god_eye_leak

Clone this wiki locally