2

I'm running into some memory issues that are causing my app to crash. On my MainActivity, I have a large bitmap in an ImageView, but the crashes are happening when I'm in other activities that also have large bitmaps.

When leaving my MainActivity to go to another Activity, should I be setting my bitmap to null?

@Override
protected void onStop() {
    super.onStop();
    myImageView.setImageBitmap(null);
}

Or can I rely on Java GC to recycle the allocated memory for the bitmap?

4 Answers 4

2

Yes, if you are OK with reloading bitmap next time your activity is restarted, it is better to recycle it in onStop. Otherwise GC will not collect it - it is attached to a view, view is attached to the activity, why would GC recycle it?

1

Make sure you resize that bitmap to the smallest possible size that's still looking fine for the purpose you need. Also deploying it in each drawable-?dpi folder with proper resize for each is important, so the system doesn't scale up even more the already large bitmap you have. This approach solved my memory crash I had when using large images for some View backgrounds.

I don't think recycling the bitmap will ensure that the GC freed the memory in time before the second activity starts with its large bitmap/s.

0

Just before you call your other activities use the finish(); method. This will close the current activity and make the resources available.

2
  • Doesn't finish() kill the activity? I want the user to be able to go back to MainActivity by pressing the back button, which wouldn't work if the activity no longer exists.
    – baekacaek
    Jun 7, 2013 at 23:14
  • It does yes, you could implement the onBackPressed method and start an intent in that method with the finish() method as well. Then only one activity will be active at a time and less resources will be used
    – Lunchbox
    Jun 7, 2013 at 23:36
0

I unbind all the views recursively using the method below. You should pass the root view and then all its childs will be cleaned. You can add more stuff to this method to recycle bitmaps, destroy webviews, clean listeners, etc!

public static void unbindDrawables(View view) {
    try {
        if (view != null) {
            if (view.getBackground() != null) {
                view.getBackground().setCallback(null);
            }
            if (view instanceof ImageView) {
                view.setImageBitmap(null);
            } else if (view instanceof ViewGroup) {
                for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                    unbindDrawables(((ViewGroup) view).getChildAt(i));
                }
                ((ViewGroup) view).removeAllViews();
            }
        }
    }
    catch (Exception e) {
        // some ViewGroups don't support the removeAllViews method
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.