日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2025/3/21 Android 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开源框架——内存泄漏检测工具 LeakCanary 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開源地址: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'}



使用說明

  • 建立RefWatcher全局變量

  • 使用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;@Overridepublic 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;@Overridepublic 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

    總結

    以上是生活随笔為你收集整理的Android开源框架——内存泄漏检测工具 LeakCanary的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。