Android中LeakCanary檢測(cè)內(nèi)存泄漏的方法
最近要對(duì)產(chǎn)品進(jìn)行內(nèi)存泄漏的檢查,最后選擇了使用Square公司開(kāi)源的一個(gè)檢測(cè)內(nèi)存泄漏的函數(shù)庫(kù)LeakCanary,在github上面搜索了一下竟然有1.6w個(gè)star,并且Android大神JakeWharton也是這個(gè)開(kāi)源庫(kù)的貢獻(xiàn)者。那么就趕快拿來(lái)用吧。
先說(shuō)一下我遇到的坑,我當(dāng)時(shí)是直接google的,然后就直接搜索到稀土掘金的一篇關(guān)于LeakCanary的介紹,我就按照他們的文章一步步的操作,到最后才發(fā)現(xiàn),他們那個(gè)build.gradle中導(dǎo)入的庫(kù)太老了,會(huì)報(bào)這樣的錯(cuò)誤Closed Failed to resolve: com.squareup.leakcanary:leakcanary對(duì)于剛使用LeakCanary的我很是費(fèi)解。
然后我就直接使用Github上的例子去引入LeakCanary https://github.com/square/leakcanary
但是又有一個(gè)問(wèn)題,就是構(gòu)建項(xiàng)目失敗,在Github上面也有說(shuō)明地址連接https://github.com/square/leakcanary/issues/815
好了說(shuō)完這些坑之后,接下來(lái)就讓我們愉快的使用LeakCanary來(lái)檢測(cè)內(nèi)存泄漏吧
1 導(dǎo)入步驟
因?yàn)椴幌胱屵@樣的檢查在正式給用戶的 release 版本中也進(jìn)行,所以在 dependencies 里添加
dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
}
接下來(lái),在你的應(yīng)用里寫(xiě)一個(gè)自定義 Application ,并在其中“安裝” RefWatcher
public class AppApplication 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);
// Normal app init code...
}
}
記得把它作為 android:name 配到 AndroidManifest.xml 的 Application 節(jié)點(diǎn)下。
上面的只能監(jiān)控Activity中的內(nèi)存,所以想要檢測(cè)Fragment中的內(nèi)存泄漏的話也是很簡(jiǎn)單只需要先在Application中保存全局的RefWatcher
public class App extends Application {
//Application為整個(gè)應(yīng)用保存全局的RefWatcher
private RefWatcher refWatcher;
@Override
public void onCreate() {
super.onCreate();
refWatcher = LeakCanary.install(this);
}
public static RefWatcher getRefWatcher(Context context) {
App application = (App) context.getApplicationContext();
return application.refWatcher;
}
}
還需要?jiǎng)?chuàng)建一個(gè)BaseFragment添加如下代碼:
public abstract class BaseFragment extends Fragment {
@Override
public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = App.getRefWatcher(getActivity());
refWatcher.watch(this);
}
}
Ok,導(dǎo)入成功后,用Debug版打包,安裝后,手機(jī)上面會(huì)自動(dòng)多一個(gè)leak的應(yīng)用,當(dāng)有內(nèi)存泄漏的時(shí)候,就會(huì)在里面顯示。這里還有一個(gè)問(wèn)題,就是在我的4.4的手機(jī)并不能出現(xiàn)那個(gè)內(nèi)存泄漏的icon。
選擇打包

導(dǎo)入成功后的icon

2 內(nèi)存泄漏解決方法
下面說(shuō)一下常見(jiàn)的幾個(gè)內(nèi)存泄漏的解決方法
1 單例 Context 內(nèi)存泄露
這里先創(chuàng)建一個(gè)很簡(jiǎn)單的單例對(duì)象
public class TestHelper {
private Context mCtx;
private TextView mTextView;
private static TestHelper ourInstance = null;
private TestHelper(Context context) {
this.mCtx = context;
}
public static TestHelper getInstance(Context context) {
if (ourInstance == null) {
ourInstance = new TestHelper(context);
}
return ourInstance;
}
}
然后我們?cè)贏ctivity中調(diào)用它,其實(shí)很多時(shí)候我們都會(huì)犯這樣的錯(cuò)誤。造成這樣錯(cuò)誤的原因很簡(jiǎn)單,就是這個(gè) ContextLeakActivity 不在了之后, TestHelper 依然會(huì) hold 住它的 Context 不放。這樣就造成了內(nèi)存泄漏。
public class ContextLeakActivity extends AppCompatActivity{
private TestHelper mTestHelper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//這里容易引起內(nèi)存泄漏
//我們?cè)?ContextLeakActivity 里獲取 TestHelper 實(shí)例時(shí)因?yàn)閭魅肓?MainActivity 的 Context,
// 這使得一旦這個(gè) Activity 不在了之后,
// TestHelper 依然會(huì) hold 住它的 Context 不放,而這個(gè)時(shí)候因?yàn)?Activity 已經(jīng)不在了,所以內(nèi)存泄露自然就產(chǎn)生了。
mTestHelper=TestHelper.getInstance(this);
//避免內(nèi)存泄漏的寫(xiě)法
// mTestHelper=TestHelper.getInstance(this.getApplication());
}
}
Context 內(nèi)存泄漏提示圖

2 Broadcast引起的內(nèi)存泄漏: 當(dāng)我們注冊(cè)過(guò)BroadcastReceiver之后,卻沒(méi)有在Activity銷毀之后,把BroadcastReceiver釋放,就很容易引起內(nèi)存泄漏,所以要在onDestroy()中銷毀BroadcastReceiver。
銷毀代碼如下
@Override
protected void onDestroy() {
super.onDestroy();
getLocalBroadcastManager().unregisterReceiver(mExitReceiver);
}
Broadcast的內(nèi)存泄漏提示圖
Ok,使用LeakLeakCanary很簡(jiǎn)單,但是解決有些內(nèi)存泄漏確實(shí)有點(diǎn)麻煩,但是不論什么樣的內(nèi)存泄漏,最關(guān)鍵的一點(diǎn)就是:在生命周期結(jié)束之前,把對(duì)象銷毀即可。如果感覺(jué)我的文章對(duì)您有用,請(qǐng)給個(gè)喜歡,謝謝
Demo下載地址連接LeakDemo_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio 4.0新特性及升級(jí)異常問(wèn)題的解決方案
這篇文章主要介紹了Android Studio 4.0新特性及升級(jí)異常的相關(guān)問(wèn)題,本文給大家分享解決方案,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android PopupWindow實(shí)現(xiàn)微信右上角的彈出菜單
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow實(shí)現(xiàn)微信右上角的彈出菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android自定義View繪制四位數(shù)隨機(jī)碼
這篇文章主要介紹了Android自定義View繪制四位數(shù)隨機(jī)碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
android 實(shí)現(xiàn)類似微信緩存和即時(shí)更新好友頭像示例
本篇文章主要介紹了android 實(shí)現(xiàn)類似微信緩存和即時(shí)更新好友頭像示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android TextView字體顏色設(shè)置方法小結(jié)
這篇文章主要介紹了Android TextView字體顏色設(shè)置方法,結(jié)合實(shí)例形式總結(jié)分析了Android開(kāi)發(fā)中TextView設(shè)置字體顏色的常用技巧,需要的朋友可以參考下2016-02-02
Flutter開(kāi)發(fā)技巧ListView去除水波紋方法示例
這篇文章主要為大家介紹了Flutter開(kāi)發(fā)技巧ListView去除水波紋方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
android中Intent傳值與Bundle傳值的區(qū)別詳解
本篇文章是對(duì)android中Intent傳值與Bundle傳值的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

