One of the most controversial features of Android is the use of Java programming language for System and User Apps. Having a Java SDK (Software Development Kit) allows rapid development of high-quality Apps, that are processor-independent. On the other hand, Java is known to make bad utilization of system resources, especially memory. Despite various code enhancements over the years, Android still struggles with bad device responsiveness and limited multitasking capabilities. Today, we’ll see how we can improve Android’s performance by tweaking the VM heap size.
A typical Java implementation relies on a tool known as Virtual Machine (VM). This is responsible to compile Java code into byte-code, and then execute the latter on the device processor. The VM uses a special memory space, the Heap space, to allocate memory to Objects and JRE classes of each App. When an App utilizes all the available heap space, the VM runs Garbage Collection: Objects that sit in the Heap space and are not referenced, are destroyed and memory used by them is freed. Garbage Collection utilizes CPU power and can affect user interface responsiveness when it is running.
On Android, the VM (Dalvik up to Android 4.x and ART from 5.x onward) uses a predefined heap size for Apps, which is passed through configuration properties inside the build.prop file. Usually, heap size is relative to the RAM size of the Android device. So, an Android device with 1 GB of RAM will normally have a heap size of 128 MB and a device with 2 GB RAM a heap size of 256 MB. However, these values do not take any usage scenarios in mind and do not perform equally well in all cases.
Heap Size Configuration
As long as an Android device is rooted, the heap size can be configured by editing the build.prop file. An easy to use App that integrates build.prop editing is Kernel Adiutor, a kernel/device manager App that is also Open Source. For more information about build.prop editing, you can read this article on DroidViews.
The two special build.prop properties that control the VM heap size are the following:
This is the heap size that Dalvik/ART assigns to every new ‘large’ App. Large Apps are the ones that include the ‘android:largeHeap’ option in their manifest. Note that many Apps abuse this option, in an effort to increase their performance.
This is the heap size that is assigned to standard Apps. This should typically be no more than half the dalvik.vm.heapsize value.
Both properties have their values expressed in MB.
Generally, setting the above values lower will increase multitasking performance. By giving less memory to each new App, more Apps could run simultaneously without triggering the Android Low Memory Killer. However, having a smaller heap size means that Garbage Collection will run more often. Keep in mind though that Garbage Collection on smaller heaps will complete faster than on larger ones.
In contrary to the above, a device used mainly for gaming would most probably benefit from large heap sizes. Games are very demanding Apps, and having more memory available to them might improve their performance. Lastly, large heap sizes should improve performance on devices where only one or two Apps are running simultaneously.
Before tweaking your Android’s heap size, take note of the following:
- Always write down the initial values of dalvik.vm.heapsize and dalvik.vm.heapgrowthlimit before tweaking. This way you can easily get back to stock configuration if need. Also, make sure you have a recovery plan in case something goes wrong.
- You need to reboot your device for the configuration changes to take effect.
- It is best to start changing values by small amounts and continue until the result is acceptable. Changes as small as 10 MB can have a great effect.
- Setting heap sizes too low might trigger an endless Garbage Collection and Android might become unstable or not usable at all.
- Both properties mentioned above need to have a ‘m’ at the end of their assigned values. For example: dalvik.vm.heapsize=96m . Forgetting to put the ‘m’ at the end will cause a bootloop.
- There are no best values for VM heap size properties. Ideal values are greatly affected by usage habits and can be found through personal experimentation.