Android內(nèi)存泄漏排查利器LeakCanary
本文為大家分享了Android內(nèi)存泄漏排查利器,供大家參考,具體內(nèi)容如下
開(kāi)源地址:https://github.com/square/leakcanary
在 build.gralde 里加上依賴, 然后sync 一下, 添加內(nèi)容如下
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'
}
省略號(hào)代表其他已有內(nèi)容
在 Application類里面將 LeakCanary 初始化。。 比如叫MyApplication ,如果沒(méi)有就創(chuàng)建一個(gè),繼承 android.app.Application。 別忘了在AndroidManifest.xml中加上,否則不起作用
public class MyApplication extends Application {
@Override
public void onCreate() {
super.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);
// 你的其他代碼從下面開(kāi)始
}
}
官方已經(jīng)有demo了,可以跑跑看。
package com.github.pandafang.leakcanarytest;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button = findViewById(R.id.async_task);
button.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
startAsyncTask();
}
});
}
void startAsyncTask() {
// This async task is an anonymous class and therefore has a hidden reference to the outer
// class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation),
// the activity instance will leak.
new AsyncTask<Void, Void, Void>() {
@Override protected Void doInBackground(Void... params) {
// Do some slow work in background
SystemClock.sleep(20000);
return null;
}
}.execute();
}
}
進(jìn)入主界面按下按鈕, 再按返回鍵退出主界面, 反復(fù)幾次,LeakCanary 就能探測(cè)到內(nèi)存泄漏了。注意要多操作幾次,1次的話泄漏規(guī)模太小,可能不會(huì)探測(cè)到。LeakCanary 一旦探測(cè)到會(huì)彈出提示的。
回到桌面,會(huì)看到一個(gè)LeakCanary 的圖標(biāo),如果有多個(gè)app 用到就會(huì)有多個(gè)LeakCanary圖標(biāo)。

點(diǎn)進(jìn)去就會(huì)看到內(nèi)存泄漏記錄

再點(diǎn)進(jìn)去就可以看到調(diào)用棧了

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android LeakCanary的使用方法介紹
- Android中的LeakCanary的原理詳解
- Android 進(jìn)階實(shí)現(xiàn)性能優(yōu)化之OOM與Leakcanary詳解原理
- Android LeakCanary檢測(cè)內(nèi)存泄露原理
- Android中LeakCanary檢測(cè)內(nèi)存泄漏的方法
- 使用Android Studio檢測(cè)內(nèi)存泄露(LeakCanary)
- Android性能優(yōu)化之利用強(qiáng)大的LeakCanary檢測(cè)內(nèi)存泄漏及解決辦法
- 詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn)
相關(guān)文章
Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫View效果的思路代碼
這篇文章主要介紹了Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫View效果,本文給大家分享實(shí)現(xiàn)思路,通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
使用Android studio查看Kotlin的字節(jié)碼教程
這篇文章主要介紹了使用Android studio查看Kotlin的字節(jié)碼教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Flutter底部導(dǎo)航欄的實(shí)現(xiàn)方式
這篇文章主要為大家詳細(xì)介紹了Flutter底部導(dǎo)航欄的實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android使用KeyStore對(duì)數(shù)據(jù)進(jìn)行加密的示例代碼
這篇文章主要介紹了Android使用KeyStore對(duì)數(shù)據(jù)進(jìn)行加密的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
加載頁(yè)面遮擋耗時(shí)操作任務(wù)頁(yè)面--第三方開(kāi)源之AndroidProgressLayout
AndroidProgressLayout實(shí)現(xiàn)為界面添加圓形進(jìn)度條。調(diào)用setprogress()方法顯示和隱藏進(jìn)度條,這篇文章主要介紹了加載頁(yè)面遮擋耗時(shí)操作任務(wù)頁(yè)面--第三方開(kāi)源之AndroidProgressLayout的相關(guān)資料,需要的朋友可以參考下2015-11-11
Android studio2.3.3升級(jí)到3.1.2坑(小記)
這篇文章主要介紹了Android studio2.3.3升級(jí)3.1.2坑(小記),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Android編程之菜單的實(shí)現(xiàn)方法實(shí)例詳解
這篇文章主要介紹了Android編程之菜單的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了上下文菜單、選項(xiàng)菜單和子菜單的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-11-11

