I am writing an image processing tool and, for reasons of image quality, I want to edit the image as large as possible.
I need to be careful that I do not have enough memory while my application is running. In addition, I want to use devices with a lot of memory. For example, I would like to support medium-sized images on less powerful devices such as the G1, and allow more powerful devices like the Samsung Galaxy S to support large images.
Is there a reliable way to estimate how much memory my application can use?
My current idea is that when determining the size of the image to download, the device can calculate the size of the image at which the memory usage for the application will be as close as possible to about 70% (i.e. not 100%, since you do not want the application to crash small allocations later or due to memory fragmentation)
I found that I can use the following code to return the percentage of available memory:
double totalMemoryUsed = (Runtime.getRuntime().totalMemory() + android.os.Debug.getNativeHeapAllocatedSize());
int percentUsed = (int)(totalMemoryUsed / Runtime.getRuntime().maxMemory() * 100);
New bitmaps are added to the portion selected by the native heap. It seems reliable in that my application will predictably crash with a memory error when percentUsed exceeds 100%. Are there any warnings for the above calculation that I should be aware of?
source
share