I have an image cache in my application which is implemented using SoftReferences. Dalvik starts applications with relatively small heap, and then increases it in case of demand. But I'd like to have my heap size bigger from the beginning. That is because when I already have some images in the cache, and an activity starts (for example) or other peak memory demand occurs, my cache gets purged in order to let memory for that peak demand. As a result, after the peak is gone I still have 2-3 MB of free space but my cache is empty!
The solution I see for this trouble is pre-allocating a bigger heap forehand so even with the peak consumption of 2-3 MBs it still has some roomspace so my SoftReferences are not purged.
I found that VMRuntime.getRuntime().setMinimumHeapSize(BIGGER_SIZE)
would be helpful. In particular, Google uses that in their apps, as mentioned here. However, VMRuntime class is marked deprecated and said to be removed from the public API in a future release. So setMinimumHeapSize
is not a permanent solution.
How then I make Dalvik to grow my heap at startup?
Currently I use a really straight-forward and cheesy technique by just allocating a large array and releasing it. This makes Dalvik grow the heap as I want. However, I'm sure there must be more elegant way of doing that. Could you tell me that, please?