Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

Drawables natively leak memory?

I'm reading this question because I have to load a ton of downloaded resources into an app I'm writing, and was curious if there was a dramatic performance hit in doing so [vs. having them in the .apk], and the "correct" answer to the question implies that while there is no performance degradation, you have to worry about releasing the memory back when you're done with it, lest it leak.

Can anyone confidently confirm or deny this? My impression was that a loaded Drawable was GCed just like everything else when the Activity it was cleaned up. I'd very much like to know if that's not true, and what the most reliable way to manually collect the memory in said instance is.

Also, does anyone know if there's a noticeable performance hit in loading images from the SDCard, vs. from the phone's memory. I'm not an electrical engineer, so, intuitively, it seems like since this is all solid state memory, it should all get read at about the same pace, but I'd love to get a definitive answer.

Upvotes: 2

Views: 658

Answers (1)

haseman
haseman

Reputation: 11313

Quick answer:

Bitmaps take two passes of the garbage collector to clean up. The first pass releases the Java object, the second pass the native pixel data. They don't leak, but you can run out of memory between when you null the pointers and the GC hits its second pass over them. This is true no matter what resource they come from. It's always a good idea to call recycle() on a bit map when you're sure both you, and the system, are done with them. Gingerbread is particularly bad in dealing with out of memory issues and bitmaps due to a bug in the Dalvik VM.

In my experience, loading images out of the apk is MUCH faster than off the SD card. 1) They're zip aligned in the apk (if you align your apk, which you should) 2) Different phones have different access times to the SD card. The general rule is, if it's on the sd card, it's going to load SLOWLY. You can get away with loading drawables from the internal memory on the main thread (even though it's a bad idea). You cannot load anything from the SD card on the main thread. Ever :-\

If I were you, I'd be as lazy as possible when loading images, I'd keep them in the apk if possible.

Upvotes: 1

Related Questions