日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

LeakCanary——直白的展现Android中的内存泄露

發布時間:2023/12/13 Android 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeakCanary——直白的展现Android中的内存泄露 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前碰到的OOM問題,終于很直白的呈現在我的眼前:我嘗試了MAT,但是發現不怎么會用。直到今天終于發現了這個新工具:

當我們的App中存在內存泄露時會在通知欄彈出通知:

當點擊該通知時,會跳轉到具體的頁面,展示出Leak的引用路徑,如下圖所示:

LeakCanary 可以用更加直白的方式將內存泄露展現在我們的面前。

以下是我找到的學習資料,寫的非常棒:
1、LeakCanary: 讓內存泄露無所遁形
2、LeakCanary 中文使用說明

AndroidStudio (官方)上使用LeakCanary 請移步:
https://github.com/square/leakcanary

Eclipse 上使用LeakCanary 請移步我的:
https://github.com/SOFTPOWER1991/LeakcanarySample-Eclipse

Android studio (自己弄的)上使用LeakCanary也可以看這個:

leakcanarySample_androidStudio

工程包括:

  • LeakCanary庫代碼
  • LeakCanaryDemo示例代碼
  • 使用步驟:

  • 將LeakCanary import 入自己的工程

  • 添加依賴:

    compile project(':leakcanary')

  • 在Application中進行配置

    public class ExampleApplication extends Application { ...... //在自己的Application中添加如下代碼 public static RefWatcher getRefWatcher(Context context) { ExampleApplication application = (ExampleApplication) context .getApplicationContext(); return application.refWatcher; } //在自己的Application中添加如下代碼 private RefWatcher refWatcher; @Override public void onCreate() { super.onCreate(); ...... //在自己的Application中添加如下代碼 refWatcher = LeakCanary.install(this); ...... } ..... }
  • 在Activity中進行配置

  • public class MainActivity extends AppCompatActivity { ...... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //在自己的應用初始Activity中加入如下兩行代碼 RefWatcher refWatcher = ExampleApplication.getRefWatcher(this); refWatcher.watch(this); textView = (TextView) findViewById(R.id.tv); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startAsyncTask(); } }); } private void async() { startAsyncTask(); } private 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(); } }

    ?

  • 在AndroidMainfest.xml 中進行配置,添加如下代碼
  • <serviceandroid:name="com.squareup.leakcanary.internal.HeapAnalyzerService"android:enabled="false" android:process=":leakcanary" /> <service android:name="com.squareup.leakcanary.DisplayLeakService" android:enabled="false" /> <activity android:name="com.squareup.leakcanary.internal.DisplayLeakActivity" android:enabled="false" android:icon="@drawable/__leak_canary_icon" android:label="@string/__leak_canary_display_activity_label" android:taskAffinity="com.squareup.leakcanary" android:theme="@style/__LeakCanary.Base" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

    轉載于:https://www.cnblogs.com/ldq2016/p/6635783.html

    總結

    以上是生活随笔為你收集整理的LeakCanary——直白的展现Android中的内存泄露的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。