Skip to content

Instantly share code, notes, and snippets.

@japgolly
Created April 17, 2013 02:21
Show Gist options
  • Save japgolly/5401310 to your computer and use it in GitHub Desktop.
Save japgolly/5401310 to your computer and use it in GitHub Desktop.
@Override
protected void onStop() {
// Release memory held by background bitmaps
AndroidUtils.releaseBackgroundImageMemory(root);
AndroidUtils.releaseBackgroundImageMemory(decorFrame);
super.onStop();
}
/**
* Releases memory held by a view's background image.
* <p>
* On Android 4.2 you'll also need to turn off hardware acceleration else the memory won't be released. Instead of
* Bitmaps you'll just end up with GLES20DisplayList instances consuming the exact same amount of memory.
*/
@SuppressWarnings("deprecation")
public static void releaseBackgroundImageMemory(View v) {
final Drawable d = v.getBackground();
if (d != null) {
d.setCallback(null);
if (d instanceof BitmapDrawable) {
BitmapDrawable b = (BitmapDrawable) d;
Bitmap bitmap = b.getBitmap();
if (bitmap != null) {
bitmap.recycle();
}
}
}
v.setBackgroundDrawable(null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment