3

In our program wo use lots of images,we unbindDrawables in eyery Activity and Fragment as below

protected void unbindDrawables(View view) {
    if (view != null) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            imageView.setImageDrawable(null);
        }
        if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            if (!(view instanceof AbsSpinner) && !(view instanceof AbsListView)) {
                ((ViewGroup) view).removeAllViews();
            }
        }
    }
}

like this https://stackoverflow.com/a/6779067 said,but I use the mat find that the bitmap also use a lots of memory and not release.

Class Name                               |   Objects | Shallow Heap | Retained Heap

 android.graphics.Bitmap                 |       490 |       23,520 | >= 36,267,912
 android.graphics.Bitmap$1               |         1 |            8 |          >= 8
 android.graphics.Bitmap$2               |         0 |            0 |         >= 80
 android.graphics.Bitmap$BitmapFinalizer |       490 |        7,840 |      >= 7,840
 android.graphics.Bitmap$CompressFormat  |         3 |           72 |        >= 232
 android.graphics.Bitmap$CompressFormat[]|         1 |           24 |         >= 24
 android.graphics.Bitmap$Config          |         4 |           96 |        >= 360
 android.graphics.Bitmap$Config[]        |         2 |           72 |         >= 72
 android.graphics.BitmapFactory          |         0 |            0 |         >= 80
 android.graphics.BitmapFactory$Options  |         0 |            0 |              
 android.graphics.BitmapRegionDecoder    |         0 |            0 |         >= 48
 android.graphics.BitmapShader           |         9 |          216 |     >= 15,736
 Total: 12 entries (4,509 filtered)      |     1,000 |       31,848 |              

I don't know why it not be released and how to release it,any body can help me,thanks a lot!

Community
  • 1
  • 1
telentqq
  • 31
  • 1
  • 1
  • 3
  • This link will help you dude : http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object – Mani Nov 29 '13 at 08:47

3 Answers3

6

You must call bitmap.recycle() to release the native memory allocated when you decoded the bitmap.

And of course you must have to take care of the life cycle of the bitmap. To release it at proper time. A better solution is to use some image loader class to take care of it. See https://github.com/nostra13/Android-Universal-Image-Loader

Robin
  • 10,052
  • 6
  • 31
  • 52
  • 1
    if I use bitmap.recycle() ,there will be another problem "trying to use a recycled bitmap ",and this article(https://developer.android.com/training/displaying-bitmaps/manage-memory.html) said "On Android 2.3.3 (API level 10) and lower, using recycle() is recommended",So when api level >10,I don't think we need call recycle.@Robin@Mac_Focha – telentqq Nov 29 '13 at 08:48
  • You must take care of the life cycle of the bitmap. To release it at proper time. A better solution is to use some image loader class to take care of it. See https://github.com/nostra13/Android-Universal-Image-Loader – Robin Nov 29 '13 at 09:29
0

call recycle on the bitmap and remove any references to your bitmap objects

Maciej Boguta
  • 1,354
  • 1
  • 12
  • 15
0

The thing is that in the pre-Honeycomb versions of Android the memory for bitmaps was (is) allocated from unmanaged memory, which creates all sorts of problems. It is still released but from the finalizer of the bitmap object implementation. Which means that it will take at least 2 passes of GC to collect it. Also if for whatever reason the finalizer fails to execute - you got the picture. Another thing is - it is really difficult to trace - DDMS does not see it and neither does MAT.

For Android 3.0 this has been changed and bitmaps are implemented over managed byte arrays, but for the older phones...

kevoroid
  • 5,052
  • 5
  • 34
  • 43
Yogesh Tatwal
  • 2,722
  • 20
  • 43