Search code examples
androidmemorybitmaprecycle

Recycle work good or not? in onDestroy()


I have question about use my recycle bitmap in imageview.

I use this code for recycle image in imageview.

drawable = imglist1.getDrawable();
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        Bitmap bitmap = bitmapDrawable.getBitmap();
        bitmap.recycle();
    }

I call it in onDestroy method

@Override
    protected void onDestroy() {
    super.onDestroy();
         ...
    }

I have in imageviews images (bitmap) when open new activity (call onDestroy() )

after open new activity can't seen changes In android monitor memory.

android monitor

android monitor

How Can control if recycle work good, or how can decreas memory?

memory it's from the most part bitmaps in imageviews.

Thanks for ideas


Solution

  • After marking a bitmap as recyclable, it will only be freed when GC execute. Here you have more info: Android: Bitmap recycle() how does it work?

    But your question is pretty unclear. If you are recycling bitmaps in the onDestroy, it does not make sense because in the onDestroy all views will be freed and the reference for the bitmap too, so it will be GCed in the next execution.

    Probably the best option for you is to resample your images. The link I posted have more info.