开发者

Android bitmaps, recycling, and heap fragmentation

I have an app that makes heavy use of bitmaps read in from the SD 开发者_JAVA百科card. I'm 99% sure my app is never destroyed without recycling these. They are all allocated into an LRU cache (commons-collections) which recycles them as they are purged from the LRU cache, and I recycle the remaining in onDestroy().

Nonetheless, I still have problems. If I start / stop / start / ... my app several times, I get out of memory error, always.

My theory on this is that it has to do with Android's non-compacting heap. After several runs, the heap is sufficiently fragmented so although there is plenty of free memory, there's no contiguous chunks big enough to load the bitmaps. That's the theory anyway.

I've been "solving" this by killing my process in onDestroy() (android.os.Process.killProcess()). This solves the problem, but introduces other issues of its own as I have other threads and services that need to complete, and waiting for all of those nicely is not practical. Not to mention that I of course understand that this is just a really bad practice.

I've read all the posts, etc on this topic and I don't see a good solution. I'm using Thumbnails.getThumbnail(), so I'm already using scaled down versions of the images. Subsampling them further results in very poor image quality.

I'm keeping a cache of 8 "mini" thumbnails and 36 "micro" thumbnails, so I never have any more than that in memory at a time. These should all fit in a few megs at very most.

What can I try next?


You say that you recycle them in onDestroy(), but perhaps you should recycle them in onPause()? I've run into some out of memory issues when switching orientation and the issue was solved by doing recycling in onPause().

Just a thought. Not sure if that'll solve your issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜