Skip to content Skip to sidebar Skip to footer

Android: Cleaning Up Memory On App Destroy

I am developing an app which instantiates a bunch of bitmap objects (e.g. buttons, which have cache bitmaps, so they don't have to get rendered again and again) Now, I realised tha

Solution 1:

Views don't have an onDestroy method because views usually don't get destroyed, activities do. A view won't just be destroyed if nothing happens to its activity (Unless you inflate a different layout... That's not the case, right?), and if something happens to its activity, you do have a callback getting called.

If there is a recycle() method, make sure you call it. And remove all reference to memory taking objects in the onDestroy, i.e:

@OverridepublicvoidonDestroy() {
    object1 = null;
    object2 = null;
    //...
}

So the GC can do its job. I had the same problem with the AdView of AdMob, although they did have a destroy method it didn't really help. But deleting my references of the view fixed the problem.

Solution 2:

Provide more information about where are you using your bitmaps, i have some serious experience of working with images and saving memory.

For example in my app i have a list of some data, which display some bitmap in each row. I store my list in a fragment(for fragment support i use compatibility library), and i recycled my bitmaps on this fragment onDestroy method.

Later i decided to optimize my list, so i added scroll listener to my list and started recycling bitmaps, when they are scrolled off the screen.

Post a Comment for "Android: Cleaning Up Memory On App Destroy"