2

I knw this is one of the most discussed question but I couldn't figure out my way with the questions available here.

I'm decoding the bitmap as below

BitmapFactory.decodeFile(sdCardPath);

While executing above line randomly system running out of memory. This is not happening always . For example if I try to decode same image 3 times, it might go out of memory 3rd time or even 4th time. This error observed randomly.

How to solve this issue??

Thanks for your time in advance

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148

2 Answers2

1

The Bitmap that you get back probably isn't garbage-collected because of a lingering reference.. Try to use the same reference if the app's design allows, or reusing a small number or references...

P Varga
  • 19,174
  • 12
  • 70
  • 108
  • This is a very generic answer to any of the out of memory scenario. It doesn't help :( and I don't have much references as u suspect. – Jay Mayu Dec 06 '11 at 04:35
1

First, if this is a bitmap that you are referencing from your assets, I would recommend moving it to your res/drawables folder, and access it as a drawable. If this is not the case, you are making too many references to this image without, as @VargaPeter pointed out, garbage collecting. The best way to garbage collect when using multiple bitmaps is to call recycle(), however System.gc() is often used as well (though in practice, you should use recycle(). If you are still having problems, you must either (a) allocate more space using the Android NDK (discouraged), or (b) use a smaller bitmap image (recommended). I know for certain there are posts discussing option b in several places on this site, as I had this same problem once before.

Phil
  • 35,852
  • 23
  • 123
  • 164