android bitmaps, recycling, and heap fragmentation
<p>i have an app that makes heavy use of bitmaps read in from the SD 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().</p>

<p>nonetheless, i still have problems. if i start / stop / start / ... my app several times, i get out of memory error, always.</p>

<p>my theory on this is that it has to do w/ 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.</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>any ideas / tips / pointers are appreciated. i'm at my wit's end on this one.</p>
