Reputation: 343
I'm getting out-of-memory errors when using setImageDrawable to update an ImageView image from resources. Most other stackoverflow posts I've seen are way more complicated than what I'm trying to do. For simplicity sake, I'm going to try to make this as simple a question as possible.
My app has about 30 image resources that I want to load into an ImageView at different times. To really cut it down, here's the single line of offending code:
((ImageView)findViewById(R.id.myimageview)).setImageDrawable(getResources().getDrawable(R.drawable.somerandomdrawable));
As I press various buttons, this line of code gets executed to update myimageview to show the new image. Eventually, my app blows up due to out-of-memory. I can't understand why Android isn't releasing/recycling the underlying drawable/bitmap. At least I can't see where the strong reference occurs here.
It's really not an option to load these bitmaps into memory (30 bitmaps each 640x640 8 bit RGB) since it will immediately blow up the app.
Upvotes: 0
Views: 2272
Reputation: 343
Ok, thought I'd answer my own question here. Turns out my app was designed poorly, and had initially loaded up quite a few bitmaps. My heap usage after load was over 59Mb! That only left about 6 Mb to play with.
So, when trying to load/replace certain imageView images with resources that expand to roughly 1.6Mb apiece (640x640 PNG images), if I did this fairly quickly I could cause an out-of-memory situation before the garbage collector had a chance to reclaim memory.
The android functions were releasing memory like they're supposed to, they just didn't get a chance to be garbage collected.
Anyway, hopefully this will help somebody else. Thanks for the input, it helped point me in the right direction.
Upvotes: 1
Reputation:
have you tried it in this way?
ImageView image = (ImageView)findViewById(R.id.myimageview);
image.setImageDrawable(getResources().getDrawable(R.drawable.somerandomdrawable));
Upvotes: 1
Reputation: 1704
I had a similar problem of out of memory errors after research i tried picaso and the problem is gone http://square.github.io/picasso/
Upvotes: 0