0

I had a user comment that after viewing a bunch of images in my app, it crashes (he believes that it is due to out of memory error). I have the following relevant code:

    int themeID = mNav[mPos];
    String icon = getThemeData(DbAdapter.KEY_ICON, themeID);
    ImageView viewer = (ImageView)findViewById(R.id.viewer);

    Bitmap bMap = null;
    try {
        bMap = getJPG(icon + ".jpg");
    } catch (IOException e) {
        e.printStackTrace();
    }


    viewer.setImageBitmap(bMap);

That gets reran as the user flips between images. From here I see that you should call recycle() on bitmaps. Do i need to call it on bMap after setting the image? Or is there some way to pull it from viwer prior to setting the next one?

According to the documentation for recycle (if I call it on bMap) it appears I don't need to use it: This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

Community
  • 1
  • 1
easycheese
  • 5,579
  • 9
  • 47
  • 83

1 Answers1

0

If you need to explicitly call recycle() it probably means that you have memory leak. Calling it is almost never a solution.

Did you try to check your app for potential mmory leak?

To check it you can for example rotate your device a few times and check how the Garbage Collector behaves. You should have something like GC_... freed 211K, 71% free 300K/1024K, external 0K/0K, paused 1ms+1ms in your LogCat nearly every time you rotate. Watch for changes in this part: 300K/1024K. If you don't have memory leaks, the first part should grow and then get smaller after a few GCs. If you have a memory leak, it will grow and grow, to the point of OOM error.

Check out my other answer about a memory leak.

If you're sure you don't have a leak and you're operating on Honeycomb you can increase the heap size accessible for your app like this: android:largeHeap="true" but it's only recommended when you deal with some huuuge bitmaps or videos, so don't overuse it.

Community
  • 1
  • 1
Michał Klimczak
  • 11,170
  • 7
  • 55
  • 92
  • Tried the rotation. Doesn't appear to be a memory leak according to your terms. Goes from 5000 to 7000 and back to 5000 and then bounces around after rotating 20-30 times. – easycheese May 28 '12 at 15:56
  • Yeah, looks like it's ok. Than maybe it's not an OOM error? How does he know it's one? He had a FC? Maybe tell him to copy the report and send it to you (it's a stack trace)? I'm not sure if report appears on all systems, though. – Michał Klimczak May 28 '12 at 16:14