Android开源框架——内存泄漏检测工具 LeakCanary

本文介绍了Android内存泄漏检测工具LeakCanary的开源地址、配置步骤及使用方法,包括如何在Gradle中配置,如何创建全局RefWatcher,以及如何监控Activity的内存泄漏。此外,还推荐了多篇关于LeakCanary的深入解读文章,帮助开发者更好地理解和使用此工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开源地址:https://github.com/square/leakcanary
FAQ : https://github.com/square/leakcanary/wiki/FAQ

配置
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>

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

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



推荐的好文章:
【1】http://www.jianshu.com/p/0049e9b344b0
【2】http://blog.kamidox.com/android-memory-guide.html
【3】https://corner.squareup.com/2015/05/leak-canary.html
【4】http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/
【5】http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0510/2860.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值