dnkoutso
dnkoutso

Reputation: 6061

Android OutOfMemoryException on createScaledBitmap and the role of SoftReferences?

I got an OOM. I know it has been covered alot by previous questions but mine has to do with the internals of Android and Java in general.

As I am loading images at random points I get this dreaded OOM exception.

I do have my images in an HashMap>. From the SoftReferences definition I would expect the Drawables to be GC'ed if memory is not enough.

In contrast, from my previous research I see that the bitmap in the drawable is allocated in a different heap (the native heap) than my applications heap. That explains why on DDMS despite the fact I see 6MB of memory I am using, my app still crashes.

It also seems that SoftReferences are "perfect" for caches and are recommended by Android engineers to use.

My question is, since my application heap never reaches the MAX point, this should mean my SoftReferences are never GC'ed.

How can I resolve this problem? Is there really any gains from using SoftReferences then? Am I not understanding something correctly?

Thanks!

Upvotes: 0

Views: 1551

Answers (2)

Paulina D.
Paulina D.

Reputation: 390

Have you tried using the recycle() method? This sets your bitmap as useless and thus the garbage collector can do its job.

Upvotes: 0

user468311
user468311

Reputation:

I had the same problem, I used SoftReference HashMap, but I still received OOM error. The only way I was able to get rid of it was not to use big images, because BitmapFactory.decodeStream need's a lot of memory to decode image.

Upvotes: 0

Related Questions