android - 使用 LeakCanary 在 AppCompatActivity 上发生内存泄漏

标签 android memory-leaks leakcanary

我使用 LeakCanary,不幸的是发生了泄漏,这是 logcat:

05-10 18:13:00.377    9098-9965/com.ponnex.justdrive D/LeakCanary﹕ In com.ponnex.justdrive:1.0:1.
* com.ponnex.justdrive.DebuggingActivity has leaked:
* GC ROOT static android.support.v4.content.LocalBroadcastManager.mInstance
* references android.support.v4.content.LocalBroadcastManager.mReceivers
* references java.util.HashMap.table
* references array java.util.HashMap$HashMapEntry[].[51]
* references java.util.HashMap$HashMapEntry.key
* references com.ponnex.justdrive.DebuggingActivity$3.this$0 (anonymous class extends android.content.BroadcastReceiver)
* leaks com.ponnex.justdrive.DebuggingActivity instance
* Reference Key: 4fea07d9-9369-4618-a8e0-9e63b3e1b908
* Device: samsung samsung GT-I9100 pa_i9100
* Android Version: 5.1.1 API: 22
* Durations: watch=5219ms, gc=244ms, heap dump=4978ms, analysis=19968ms

这是我的调试 Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
LeakCanary.install(getApplication());
setContentView(R.layout.activity_debugging);
activityTV = (TextView) findViewById(R.id.debugText);
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(screenReceiver, new IntentFilter("com.ponnex.justdrive.ActivityRecognitionIntentService"));

}
private BroadcastReceiver screenReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String activity = intent.getStringExtra("Activity");
        updateUI(activity);
    }
};

public static void updateUI(String activity) {
    activityTV.setText(activity);
}

我一直在检查我的 Activity 并追踪它,但我不知道如何解决这个问题,有什么帮助吗? :D

如果您需要我的 Activity 代码的更多信息,我很乐意在此处添加或发布它们;)

---编辑---

我还从另一个引用 snackbar( Snackbar by Nispok ) 的 Activity 中泄漏

05-10 18:27:43.161   9098-16222/com.ponnex.justdrive D/LeakCanary﹕ In com.ponnex.justdrive:1.0:1.
* com.ponnex.justdrive.MainActivity has leaked:
* GC ROOT static com.nispok.snackbar.SnackbarManager.currentSnackbar
* references com.nispok.snackbar.Snackbar.mContext
* leaks com.ponnex.justdrive.MainActivity instance
* Reference Key: 1982a1a8-e66e-4218-9b5a-8f907ee26a7f
* Device: samsung samsung GT-I9100 pa_i9100
* Android Version: 5.1.1 API: 22
* Durations: watch=5291ms, gc=315ms, heap dump=4443ms, analysis=21801ms

最佳答案

需要调用LocalBroadcastManager.unregisterReceiver方法。

LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(screenReceiver)

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

关于android - 使用 LeakCanary 在 AppCompatActivity 上发生内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30150272/

相关文章:

android - 无法使用测试帐户 "The publisher cannot purchase this item"进行 Android 应用内购买

android - 如何在 Android 上显示大量文本

memory-leaks - MPI_REDUCE 导致内存泄漏

linux - alsa - 内存泄漏?

android - DisplayLeakActivity 不存在

android - 为什么 android 不允许我在我自己的线程中更新我的 UI

android - 如何在android中将字符串搜索到字符串生成器中

javascript - 我应该在更改 div 内容之前删除事件吗?

android - 使用 LeakCanary 在 DeathMonitor 上发生内存泄漏

android - 添加 fragment 并调用 .replace() 导致添加的 fragment 中的内存泄漏