If you must use (and retain) a large amount of memory, then yes, you can and should use android:largeHeap="true"
. But if you do use it, you should be prepared to have your app flushed from memory whenever other apps are in the foreground.
By "be prepared," I mean that you should design for that likelihood, so that your onStop()
and onResume()
methods are written as efficiently as possible, while ensuring that all pertinent state is saved and restored in a manner that presents a seamless appearance to the user.
There are three methods that relate to this parameter: maxMemory()
, getMemoryClass()
, and getLargeMemoryClass()
.
For most devices, maxMemory()
will represent a similar value to getMemoryClass()
by default, although the latter is expressed in megabytes, while the former is expressed in bytes.
When you use the largeHeap
parameter, maxMemory()
will be increased to a device-specific higher level, while getMemoryClass()
will remain the same.
getMemoryClass()
does not constrain your heap size, but it tells you the amount of heap you should use if you want your app to function comfortably and compatibly within the limits of the particular device on which you are running.
maxMemory()
, by contrast, does constrain your heap size, and so you do gain access to additional heap through increasing its value, and largeHeap
does increase that value. However, the increased amount of heap is still limited, and that limit will be device-specific, which means that the amount of heap available to your app will vary, depending on the resources of the device on which your app is running. So, using largeHeap
is not an invitation for your app to abandon all caution and oink its way through the all-you-can-eat buffet.
Your app can discover exactly how much memory would be made available on a particular device through using the largeHeap
parameter by invoking the method getLargeMemoryClass()
. The value returned is in megabytes.
This earlier post includes a discussion of the largeHeap
parameter, as well as a number of examples of what amounts of heap are made available with and without its usage, on several specific Android devices:
Detect application heap size in Android
I have not deployed any of my own apps with this parameter set to true. However, I have some memory-intensive code in one of my apps for compiling a set of optimization-related parameters, that runs only during development. I add the largeHeap
parameter only during development, in order to avoid out of memory errors while running this code. But I remove the parameter (and the code) prior to deploying the app.