使用LeakCanary在AppCompatActivity上泄漏内存

时间:2021-08-28 00:30:00

I use LeakCanary and unfortunately got a leak and here is the logcat:

我使用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

This is my DebuggingActivity

这是我的DebuggingActivity

@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);
}

I've been checking my activity and tracing it out but I don't know how to fix this any help? :D

我一直在检查我的活动并追查它,但我不知道如何解决这个问题? :d

If you need more from my activity code I'll be glad to add or post them here ;)

如果您需要更多我的活动代码,我很乐意在这里添加或发布它们;)

---EDIT---

I also got a leak from another activity refering to snackbar(Snackbar by Nispok)

我也从另一个活动中泄漏了一些零食吧(Snispbar by Nispok)

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

1 个解决方案

#1


You need to call LocalBroadcastManager.unregisterReceiver method.

您需要调用LocalBroadcastManager.unregisterReceiver方法。

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

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

#1


You need to call LocalBroadcastManager.unregisterReceiver method.

您需要调用LocalBroadcastManager.unregisterReceiver方法。

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

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