Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is BitmapRegionDecoder allocating so much memory?
    primarykey
    data
    text
    <p>I have 1280x1280 JPGs on disk and I'm using the following code to decode a region of the image (clipping off the sides) to display in an ImageView. There are similar issues in clipping the top and bottom off the image.</p> <pre><code>private Bitmap decodeBitmapRegion(InputStream in, Rect region, Bitmap recycleBitmap) { LOGD(TAG, "decodeBitmapRegion region=" + region + ", recycleBitmap=" + recycleBitmap ); Bitmap bitmap = null; BitmapRegionDecoder decoder = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 1; options.inBitmap = recycleBitmap; try { decoder = BitmapRegionDecoder.newInstance(in, false); bitmap = decoder.decodeRegion(region, options); } catch (IllegalArgumentException e){ LOGE(TAG, "Failed to recycle bitmap for rect=" + region, e); } catch (IOException e) { LOGE(TAG, "Failed to decode into rect=" + region, e); } finally { if (decoder != null) decoder.recycle(); } return bitmap; } </code></pre> <p>On an Acer Iconia A100 (4.0.3) the code works perfectly and decodes the region into the recycle bitmap if provided, allocating no memory on the heap. The logcat has no GC or grow heap messages. It's also working correctly on a Nexus 7 running 4.2.0.</p> <p>On a Motorola Xoom (4.0.4) the code generates the following logcat. The first dalvikvm-heap grow (4738256 bytes) is presumably for the BitmapRegionDecoder's data and the second (3603216 bytes) matches the amount of memory required to store the image (800x1126x4). There's also a skia error. </p> <pre><code> 8522 LocationImageLoader D decodeBitmapRegion region=Rect(240, 77 - 1040, 1203), recycleBitmap=android.graphics.Bitmap@415319a8 8522 dalvikvm D GC_FOR_ALLOC freed 8213K, 28% free 22782K/31623K, paused 34ms 8522 dalvikvm-heap I Grow heap (frag case) to 26.808MB for 4738256-byte allocation 8522 dalvikvm D GC_CONCURRENT freed 6K, 14% free 27403K/31623K, paused 3ms+7ms 8522 skia D WARNING: One-shot allocator has already allocated (alloc count = 1) 8522 dalvikvm D GC_FOR_ALLOC freed 0K, 14% free 27403K/31623K, paused 33ms 8522 dalvikvm-heap I Grow heap (frag case) to 30.238MB for 3603216-byte allocation 8522 dalvikvm D GC_FOR_ALLOC freed 0K, 3% free 30922K/31623K, paused 41ms </code></pre> <p>Setting options.inPreferredConfig = Bitmap.Config.RGB_565; seems to negate the second allocation on the Xoom, as if the bitmap is recycled correctly, but there's still memory allocated for the decoder.</p> <p>Recycling with the full images (1280x1280) and BitmapFactory works though significantly more memory is used to store the full image than would be required by the region.</p> <p>I can't understand why the Xoom doesn't work but the Acer is fine, when both are running Android 4.0.x.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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