国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android內(nèi)存泄漏排查利器LeakCanary

2019-10-23 18:33:47
字體:
供稿:網(wǎng)友

本文為大家分享了Android內(nèi)存泄漏排查利器,供大家參考,具體內(nè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' }

省略號代表其他已有內(nèi)容

在 Application類里面將 LeakCanary 初始化。。 比如叫MyApplication ,如果沒有就創(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);  // 你的其他代碼從下面開始 }}

官方已經(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  就能探測到內(nèi)存泄漏了。注意要多操作幾次,1次的話泄漏規(guī)模太小,可能不會(huì)探測到。LeakCanary  一旦探測到會(huì)彈出提示的。

回到桌面,會(huì)看到一個(gè)LeakCanary 的圖標(biāo),如果有多個(gè)app 用到就會(huì)有多個(gè)LeakCanary圖標(biāo)。

Android,內(nèi)存泄漏,LeakCanary

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

Android,內(nèi)存泄漏,LeakCanary

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

Android,內(nèi)存泄漏,LeakCanary

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 尤溪县| 亳州市| 广元市| 天镇县| 二连浩特市| 来宾市| 稻城县| 阿尔山市| 澄迈县| 紫金县| 苗栗市| 大姚县| 平潭县| 思茅市| 郸城县| 古交市| 黄龙县| 兰考县| 阜康市| 个旧市| 莎车县| 台江县| 车致| 绍兴市| 独山县| 仪陇县| 祁东县| 福贡县| 安化县| 台北市| 酉阳| 平陆县| 彩票| 南充市| 河曲县| 桑日县| 从化市| 泊头市| 沽源县| 双峰县| 东乌珠穆沁旗|