0

One would think that the second approach would be more efficient but I can't say that I see any improvement using it. Is there a difference between the following two ? (memmory wise ofcourse)

Bitmap bm=MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null);
bm=cropAndScaleBitmap(bm);
//use bm

vs

Bitmap bm=MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null);
Bitmap b =cropAndScaleBitmap(bm);
bm.recycle();
//use b

2 Answers2

0

Things to note here ...

Even though you have specified recycle, it will have any effect only when the next GC is trigerred.

Other things to note

  1. In pre-Honeycomb versions of Android the memory for bitmaps was (is) allocated from unmanaged memory.It will take at least 2 passes of GC to collect it. Another thing is - it is really difficult to trace - DDMS does not see it and neither does MAT.

  2. Read this link https://developer.android.com/training/displaying-bitmaps/manage-memory.html. If api level >10,I don't think we need to call recycle.

Refer to this link ... It provides all details and the code sample for how to implement step by step.

https://developer.android.com/training/displaying-bitmaps/manage-memory.html

Hope this helps.

Prem
  • 4,823
  • 4
  • 31
  • 63
0

In your specific example (first one) you don't actually reuse the source, you just overwrite the value of the bm making it point to the new bitmap instead, in this case you can no longer call recycle on the source bitmap as you no longer have a reference to it.

I recommend using the second method to make sure the source bitmap is recycled.

Valentin Iorgu
  • 521
  • 4
  • 5