1. 程式人生 > >安卓 內存 泄漏 工具 LeakCanary 使用

安卓 內存 泄漏 工具 LeakCanary 使用

集成 代碼 hub 頁面 not in red install 探測 初始化

韓夢飛沙 yue31313 韓亞飛 han_meng_fei_sha [email protected]

LeakCanary是Square開源了一個內存泄露自動探測神器 。這是項目的github倉庫地址:https://github.com/square/leakcanary 。使用非常簡單,在build.gradle中引入包依賴:

 dependencies {
   debugCompile ‘com.squareup.leakcanary:leakcanary-android:1.5‘
   releaseCompile ‘com.squareup.leakcanary:leakcanary-android-no-op:1.5‘
   testCompile ‘com.squareup.leakcanary:leakcanary-android-no-op:1.5‘
 }

在Application中的onCreate方法中增加初始化代碼:

if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for
// heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);

集成後什麽都不用做,按照正常測試,當有內存泄漏發生後,應用會通過系統通知欄發出通知,點擊通知就可以進入查看內存泄漏的具體信息。在這裏舉個實踐中的例子。把LeakCanary集成到項目中後,等App啟動後一會,系統通知到了,點擊通知,跳轉到泄漏的詳情頁面進行查看。

安卓 內存 泄漏 工具 LeakCanary 使用