Wednesday, May 9, 2012

Android: (Memory Management) memory leak - free ImageView resource

In order to free ImageView/Drawable object to gain memory space:

        ImageView v = (ImageView) _________;
        ((BitmapDrawable) v.getDrawable()).setCallback(null);
        

Usually you do it on onDestroy()

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}


For Bitmap, you use recycle().

No comments:

Post a Comment