Note that there are some explanatory texts on larger screens.

plurals
  1. POStill have to recycle bitmaps in Android 4+?
    text
    copied!<p>My Activity has an ImageView that takes up roughly half of the screen in portrait and a little less in landscape. I load the Bitmap using inSampleSize to be frugal with my memory, but I wasn't doing anything to explicitly clean up the memory either. I used to go crazy with recycling my Bitmaps prior to Gingerbread, but I've been more relaxed since Bitmap data was apparently moved into the Dalvik VM. Without having any cleanup code, things worked in normal use cases, but I noticed that I'd run out of memory if I rotated this Activity a bunch of times. So, I added the kind of aggressive memory cleanup code that you'd see prior to Gingerbread and it no longer crashes. I'm wondering if this is overkill at this point though. Is all of this code just equivalent to setting my Bitmap and ImageView to null in onDestroy, or is all of this extra work of tracking down underlying Bitamp objects and recycling them still valid and necessary? Here's my code that fixed my OOME crashes:</p> <pre><code>Bitmap thumbnail; @InjectView(R.id.thumbnail) ImageView image; @Override protected void onDestroy() { cleanupImageMemory(); super.onDestroy(); } private void cleanupImageMemory() { if (thumbnail != null) { thumbnail.recycle(); thumbnail = null; } if (image != null) { Drawable d = image.getDrawable(); if (d != null &amp;&amp; d instanceof BitmapDrawable) { BitmapDrawable db = (BitmapDrawable) d; if (db.getBitmap() != null) { db.getBitmap().recycle(); } } d = null; image = null; } System.gc(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload