Can we rely on our Android app getting a set amount of memory or does it vary between phone models or OS versions?
For example, I have a memory cache, and I've set its limit to 5 MB. If on one device, my app will only get 8 MB of memory to play with, and on another, 24 MB, I'd like to tune the cap of the memory cache to take advantage of more or less memory.
Is there any way to figure this out, does the amount even vary?
Yes, the maximum heap size varies from device to device. You can check the device's approximate per-application memory class by calling getMemoryClass()
.
Returns the approximate per-application memory class of the current device. This gives you an idea of how hard a memory limit you should impose on your application to let the overall system work best. The returned value is in megabytes; the baseline Android memory class is 16 (which happens to be the Java heap limit of those devices); some device with more memory may return 24 or even higher numbers.
The only built-in way to change heap size of an app is by setting android:largeHeap="true"
in the AndroidManifest.xml
. This will generally increase heap size from 48 to 128. Keep in mind this approach will only work on 3.x tablets. Otherwise, you'll need a rooted device, which is obviously not something you would want to rely on as a developer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With