zoukankan      html  css  js  c++  java
  • 安卓 内存泄漏检测工具 LeakCanary 使用

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com

    配置 
    build.gradle

    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'
     }


    使用说明

    1. 建立RefWatcher全局变量

    2. 使用RefWatcher去监控引用,Activity,Fragment等中的内存泄漏



    例子

    第一步:定义RefWathcher全局变量

    package com.app.client;
    
    import android.app.Application;
    import android.content.Context;
    
    import com.squareup.leakcanary.LeakCanary;
    import com.squareup.leakcanary.RefWatcher;
    
    
    public class MyApplication extends Application {
    
        public static RefWatcher getRefWatcher(Context context){
    
            MyApplication application = (MyApplication) context.getApplicationContext();
            return application.refWatcher;
        }
    
        private RefWatcher refWatcher;
    
        @Override
        public void onCreate() {
            super.onCreate();
            refWatcher = LeakCanary.install(this);
        }
    }
    

    第二步:监控Activity中的内存泄漏问题

    package com.app.client;
    
    import android.app.Application;
    import android.content.Context;
    
    import com.squareup.leakcanary.LeakCanary;
    import com.squareup.leakcanary.RefWatcher;
    
    
    public class MyApplication extends Application {
    
        public static RefWatcher getRefWatcher(Context context){
    
            MyApplication application = (MyApplication) context.getApplicationContext();
            return application.refWatcher;
        }
    
        private RefWatcher refWatcher;
    
        @Override
        public void onCreate() {
            super.onCreate();
            refWatcher = LeakCanary.install(this);
        }
    }
    

    Manifest配置

     <application
            android:name=".MyApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme" >
            <activity android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    第三步:生成的泄漏结果图

    这里写图片描述                   这里写图片描述

  • 相关阅读:
    input,textarea限制字数,实时绑定
    rem布局和vw布局的理解
    HTML5+CSS3响应式垂直时间轴,高端,大气
    谈谈前端工程化是个啥?
    js动态添加html标签和属性_手动插入meta、script、div、img等标签
    textarea换行_在textarea中如何换行的实现总汇
    css常用的颜色单位表示法
    CSS3 2D转换
    为什么设置overflow为hidden可以清除浮动带来的影响
    什么是数据交互格式?xml和json优缺点
  • 原文地址:https://www.cnblogs.com/yue31313/p/7396964.html
Copyright © 2011-2022 走看看