欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中LeakCanary檢測(cè)內(nèi)存泄漏的方法

 更新時(shí)間:2017年09月12日 10:22:41   作者:一本未寫(xiě)完的書(shū)  
本篇文章主要介紹了Android中LeakCanary檢測(cè)內(nèi)存泄漏的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lá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)文章

最新評(píng)論