In one of my actions, I load a large number of bitmaps dynamically and display them in a GridView. There is a risk of heap oversizing and application crashes. I need a reliable way to find out when the heap size gets too large to stop the loading of the bitmap and start processing the ones that are currently being viewed. I tried using this function:
public int memoryPercent(){ Double alloc=new Double(Runtime.getRuntime().totalMemory()/1048576); Double avail=new Double(Runtime.getRuntime().maxMemory()/1048576); double per= (alloc/avail)*100; int pr=(int)per; Log.e("Memory used",Integer.toString(pr)); return pr; }
Returns a value that actually increases when loading more raster images. But this does not decrease when bitmaps are processed. What other options do I have?
I thought about calculating the byte size of each bitmap and adding them to see their total size, and then fine-tune those that are being processed, trying to keep their total size under the value that Runtime.getRuntime (). maxMemory () returns. But that sounds too dirty. there must be a better way
source share