I'm working on an app which needs to store many bitmap images in memory. No surprise, that OutOfMemoryError
is often caught on some devices, when bitmap collection becomes too big. And I need to avoid this somehow.
Saying briefly, I need a kind of collection which will behave a kind of "add items one by one, until adding next item would cause OutOfMemoryError", but I'm not experienced enough to find a proper approach. I believe, some sort of weak-reference collection should be implemented.
I like WeakHashMap
, but there is one crucial problem with it - I cannot control how items will be discarded. In my app, bitmaps are added in order of priority: the most important bitmaps, that should be stored as long as possible, go first. WeakHashMap
, as I understand, doesn't provide such prioritization.
Any working approaches or ideas?
P. S. This question is not about bitmap optimization. Imagine, there are some big objects instead of bitmaps, that cannot be compressed or optimized. The question is about storing items in memory directly controlling their priority, such that objects with low priority can be GC'd quickly or not added at all.
P. P. S. So far, I discovered two possible solutions:
1) dividing a collection in 2 parts, such that first, more prioritized, will contain items themselves (i. e. strong references), and second will contain soft references. Control of adding can be processed using Runtime.getRuntime.maxMemory()
and .totalMemory()
--when totalMemory occupied by the heap exceeds some % of maxMemory, adding new items into collection should be prohibited;
2) using a collection of soft references and track items' finalize()
--when it is invoked (i. e. corresponding object is going to be picked by GC), return this item as a soft reference back to the collection and replace another item--with least priority--by phantom reference. Theoretically, this would give more strict priority control, but I'm not sure how it will behave in practice.