Ligol Ligol - 1 year ago 167
Android Question

Memory Leak on DeathMonitor using LeakCanary

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

In com.appturbo.appoftheday2015:2.09.2:222.
* com.appturbo.appturbo.ui.HomeActivity has leaked:
* GC ROOT$DeathMonitor.this$0
* references
* leaks com.appturbo.appturbo.ui.HomeActivity instance

* Reference Key: e049c2ed-6784-4850-b794-20fa96c13dcf
* Device: motorola google Nexus 6 shamu
* Android Version: 5.1 API: 22
* Durations: watch=5176ms, gc=228ms, heap dump=4974ms, analysis=29320ms

Does some of you have already seen a leak like that? Any idea? This leak appear after:

  • Changing the Resource Configuration to switch the language

  • Finishing the activity

  • Restarting the activity

Answer Source

After digging in some bad memory search. I've found a solution to this memory leak. The problem is due to some drawable not correctly detached from the view and keeping a pointer on some other Object. Due to this the GC cannot delete these Objects, and here it is our memory leak.

In order to solve this problem, I use this code to unlink the drawable.

    public static void unbindDrawables(View view) {
    if (view.getBackground() != null) {
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        ((ViewGroup) view).removeAllViews();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download