How to increase heap size of an android application?
AndroidAndroid Problem Overview
I am writing an Android application which uses several 3D models. Such a model with textures can take up a lot of memory. I found out the manufacturer sets a limit on the heap size an application can use. For example my tablet Samsung Galaxy Tab 8.9 P7310 can take up 64MB of memory.
Is there a way to increase this size of memory an application can use?
Android Solutions
Solution 1 - Android
You can use android:largeHeap="true" to request a larger heap size, but this will not work on any pre Honeycomb devices. On pre 2.3 devices, you can use the VMRuntime class, but this will not work on Gingerbread and above.
The only way to have as large a limit as possible is to do memory intensive tasks via the NDK, as the NDK does not impose memory limits like the SDK.
Alternatively, you could only load the part of the model that is currently in view, and load the rest as you need it, while removing the unused parts from memory. However, this may not be possible, depending on your app.
Solution 2 - Android
> Is there a way to increase this size of memory an application can use?
Applications running on API Level 11+ can have android:largeHeap="true"
on the <application>
element in the manifest to request a larger-than-normal heap size, and getLargeMemoryClass()
on ActivityManager
will tell you how big that heap is. However:
-
This only works on API Level 11+ (i.e., Honeycomb and beyond)
-
There is no guarantee how large the large heap will be
-
The user will perceive your large-heap request, because it will
force their other apps out of RAMterminate other apps' processes to free up system RAM for use by your large heap -
Because of #3, and the fact that I expect that
android:largeHeap
will be abused, support for this may be abandoned in the future, or the user may be warned about this at install time (e.g., you will need to request a special permission for it) -
Presently, this feature is lightly documented
Solution 3 - Android
you can't increase the heap size dynamically.
you can request to use more by using android:largeHeap="true"
in the manifest.
also, you can use native memory (NDK & JNI)
, so you actually bypass the heap size limitation.
here are some posts i've made about it:
and here's a library i've made for it:
Solution 4 - Android
Use second process. Declare at AndroidManifest
new Service
with
android:process=":second"
Exchange between first and second process over BroadcastReceiver
Solution 5 - Android
This can be done by two ways according to your Android OS.
- You can use
android:largeHeap="true"
in application tag of Android manifest to request a larger heap size, but this will not work on any pre Honeycomb devices. - On pre 2.3 devices, you can use the VMRuntime class, but this will not work on Gingerbread and above See below how to do it.
VMRuntime.getRuntime().setMinimumHeapSize(BIGGER_SIZE);
Before Setting HeapSize make sure that you have entered the appropriate size which will not affect other application or OS functionality. Before settings just check how much size your app takes & then set the size just to fulfill your job. Dont use so much of memory otherwise other apps might affect.
Reference: http://dwij.co.in/increase-heap-size-of-android-applicationSolution 6 - Android
From what I remember you could use VMRuntime
class in early Android versions but now you just can't anymore.
I don't think letting the developer choose the heap size in a mobile environment can be considered so safe though. I think it's easier that you can find a way to modify the heap size in a specific device (not on the programming side) that by trying to modify it from the application itself.
Solution 7 - Android
Increasing Java Heap unfairly eats deficit mobile resurces. Sometimes it is sufficient to just wait for garbage collector and then resume your operations after heap space is reduced. Use this static method then.