zoukankan      html  css  js  c++  java
  • AndroidStudio MAT LeakCanary 内存分析之 LeakCanary

    现在我们换一种更清晰方便的方式:LeakCanary https://github.com/square/leakcanary

    首先将LeakCanary绑在我们的app上 
    build.gradle

    dependencies{
     dependencies {
       debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
       releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
       testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
     }
    public class SunApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            Global.mContext = SunApplication.this.getApplicationContext();
            initializeLeakDetection();
        }
    
        private void initializeLeakDetection() {
            if (BuildConfig.DEBUG) {
                LeakCanary.install(this);
            }
        }
    }

    Manifest 添加权限WRITE_EXTERNAL_STORAGE、不然会发生错误、如下代码 
    android 6.0以上另外获取权限、详情请见我的博文 Android 6.0 最简单的权限获取方法 RxPermition EasyPermition

      <!--Required by Debug.startMethodTracing() -->
        <!--LeakCanary need this Unable to resume activity
        {com.rvitemtouch.sun.sundagger2application/com.squareup.leakcanary.internal.DisplayLeakActivity}:
        java.lang.UnsupportedOperationException: Could not create leak directory
        /storage/emulated/0/Download/leakcanary-com.rvitemtouch.sun.sundagger2application-->
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    安装好了、运行app、继续将之前写的(Android AndroidStudio MAT LeakCanary 内存分析之 AndroidStudio 内存泄漏分析 Memory Monitor

    Activity反复翻转屏幕、导致内存泄漏

    出现内存泄漏提示

    这里写图片描述

    Leaks 会伴随我们的app一块install、那么打开leaks、等待一会

    这里写图片描述

    找到我们定位的泄漏 打开

    这里写图片描述
    就能看出详细的链式结构、上面写的很明白了、正如之前我们所料 
    是Memory..Activity的引用被Runnable占用了

  • 相关阅读:
    SWT DragSource 和 DropTarget 托拉拽
    Java的反射机制
    1. 算法导论
    SWT对于监听Tab键的理解
    SWT基础
    socket
    TCP/IP
    RPC(远程过程调用协议)
    Jython基本知识
    #与##
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7068767.html
Copyright © 2011-2022 走看看