android - LeakCanary使用问题:空的Activity也报内存泄漏

查看:235
本文介绍了android - LeakCanary使用问题:空的Activity也报内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

操作,打开activity,然后后退键退出,10几秒后就收到内存泄露的提示

日志:

D/LeakCanary: In com.example.shen.testleak:1.0:1.
                                             * com.example.shen.testleak.TestLeakActivity has leaked:
                                             * GC ROOT static android.media.AudioManager.mContext_static
                                             * references android.app.ContextImpl.mOuterContext
                                             * leaks com.example.shen.testleak.TestLeakActivity instance
W/ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1468 com.android.server.StatusBarManagerService.sendNotification:968 com.android.server.StatusBarManagerService.addNotification:657 com.android.server.NotificationManagerService$6.run:2115 android.os.Handler.handleCallback:733 

我的配置过程

  • 1.在 build.gradle 中加入引用

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
}

  • 2.在 Application 内配置,安装

public class MyApplication extends Application {

    private RefWatcher refWatcher;

    public static RefWatcher getRefWatcher(Context context) {
        MyApplication application = (MyApplication) context.getApplicationContext();
        return application.refWatcher;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        refWatcher = LeakCanary.install(this);
    }
}

  • 3.TestLeakActivity 的 onDestroy() 内添加watch

public class TestLeakActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        
        RefWatcher refWatcher = MyApplication.getRefWatcher(this);
        refWatcher.watch(this);//根据,回答者提醒把这段话放onDestroy后面,重新编译了一下,可惜错误依旧
    }
}

又等了一会,提示DisplayLeakActivity内存泄漏

D/LeakCanary: In com.example.shen.testleak:1.0:1.
                                               * com.squareup.leakcanary.internal.DisplayLeakActivity has leaked:
                                               * GC ROOT static android.media.AudioManager.mContext_static
                                               * references android.app.ContextImpl.mOuterContext
                                               * leaks com.squareup.leakcanary.internal.DisplayLeakActivity instance

请问是否是我的配置有问题?

解决方案

但是看日志反馈的是泄漏了啊,说不定android源码里面有泄漏呢。配置很简单,配置很难出问题。

这篇关于android - LeakCanary使用问题:空的Activity也报内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆