1

I am wondering if its possible to free resources when leaving an activity in android?

My app has several activities and one of them is very memory-consuming as it holds al lot of high quality images. If one goes to this activity heap soars and its not very far from tangent out-of-memory-exeption. Let say it occypies 85% of the limit. But - this is one of several activities and the only one that is memory-consuming. The others are not - its only listviews which hold some text.

I am not saying that I have a memory leak but to be on the secure side I would like to free resources when leaving this activity that holds the images. I cannot figure out how to do that?

thanks!!!

user2991252
  • 760
  • 2
  • 11
  • 28

3 Answers3

0

Depending on what your resources are in the onDestroy() method you should free them up and allow them to be collected.

Community
  • 1
  • 1
KDEx
  • 3,505
  • 4
  • 31
  • 39
0

Your resources should be cleared by the garbage collector automatically when they are no longer being used by the system however you can force this as shown in this answer: https://stackoverflow.com/a/7009362/1222050

ImageView imageView = (ImageView)findViewById(R.id.my_image);
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    Bitmap bitmap = bitmapDrawable.getBitmap();
    bitmap.recycle();
}

Also if you are filling that much memory I would recommend looking into scaling down the size of our bitmaps. Which you can do like this:

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));

One question I have is how are you starting the next activity/ part of the app?

Community
  • 1
  • 1
Matt Carron
  • 183
  • 3
  • 11
0

A Drawable is much more broad than a Bitmap. Drawable is intended to represent anything the graphics system can render to the display. There are subclasses of Drawable - such as as ShapeDrawable, or ColorDrawable - that do not contain Bitmaps and hence do not need any sort of manual memory management.

A BitmapDrawable wraps a Bitmap and contains a reference to a Bitmap. The Bitmap is special, because Android maintains the pixel data for all Bitmaps in a separate heap, which is managed separately from the rest of your application. When the Android garbage collector cleans up a Bitmap object, it also cleans up the the pixel data from the bitmap heap. But you can force it to happen sooner by manually calling recycle() on the Bitmap, which marks the Bitmap invalid and releases its pixel data.

Android allocates a fixed size bitmap heap for each running application, and it is possible for your app to exhaust its heap by leaving too many Bitmaps in use at once. That's why if your application uses bitmaps extensively, you will probably benefit from recycling bitmaps as soon as you know you won't need them.

Update: the separate bitmap heap applies to pre-Honeycomb versions of Android. As of Honeycomb, the bitmap heap was merged with the application heap.