Apps And Games

Building Smart Apps: Make Your App More Memory Efficient

There is no doubt that Android apps have changed the way people used to perform various tasks. But, when it comes to measure the performance of an app, it is not only the excitement or value it creates, but it is also about the amount of memory the app is utilizing.

This gets more significant when you’re creating app for Android as the memory available to general Android smartphone or tablet is considerably smaller than the memory available to other devices, such as desktops or laptops.

Here you’ll get to know how you can ensure the lowest memory consumption for your app. Let’s start.

Checking For Memory Leaks

Though Android’s Java virtual machine carries out routine garbage collection (GC), if you want your app to deliver the most remarkable user experience, there is no space for becoming contented.

The garbage collector is capable of eradicating objects that it considers inaccessible, so if your app holds onto object references needlessly, these objects won’t get garbage collected. This can lead to a memory leakage. Since Android devices are found to be low on memory to start with, a memory leak can rapidly lead to an OutOfMemoryError.

Making the situation worse, the system will try to compensate this memory leakage by stimulating more frequent GC events. This frequent occurrence of GC events will ultimately make an adverse impact on your app’s overall performance. In order to avoid this, it is always a good idea to scan your project for memory leaks and the best tool for this job is Memory Monitor.

In contrast to other Android SDK tools, Memory Monitor tends to communicate only with a running application. So you’ll be required to install the app you want to test on either an Android Virtual Device (AVD) or a physical Android device, and then ensure this application is visible onscreen the entire time you’re testing it.

You can get Memory Monitor from the primary Android Studio interface. Choose the ‘Android Monitor’ tab towards the bottom of the screen and then choose the ‘Monitors’ tab.

Once Memory Monitor detects the running application, it’ll print your app’s memory consumption in the form of a graph. The unallocated memory will be displayed in light blue color, while the used memory will be shown in dark blue color.

When you start using Memory Monitor to analyze your app’s memory usage, don’t forget to try different actions and their impact on the memory usage. Do look for sudden drops in allocated memory which is actually the signal of a GC event.

Observe your app’s GC patterns over a period of time to analyze anything unusual with your app. Particularly search for an unusual number of GC events occurring in a short period of time, or an increasing number of GC events.

If you do see any uncommon GC response, you’ll be required to procure more information about exactly what’s causing these GC events – which will bring you to the Android Device Monitor’s ‘Heap’ tab.

Analyzing The Heap

Android devices are known to have a limited amount of heap space that can only contain a particular number of objects. When the system is running low on memory, it’ll cause a GC event, so if you spot uncommon GC activity, the next thing you should do is to have a closer look at the type of elements your app is assigning on the heap.

To access the ‘Heap’ tab, open the Android Device Monitor, choose ‘Tools’ from Android Studio’s menu bar, and then select ‘Android’ and ‘Android Device Monitor.’

After the Android Device Monitor is launched, make sure that the ‘DDMS’ button is chosen. Now, in the ‘Devices’ panel, pick the device and the process you wish to inspect.

Open the ‘Heat’ tab, and tap on the ‘Update Heap’ icon.

You will get to know about your app’s heap usage in the ‘Heap’ tab only after the occurrence of a GC event. So, you need to exhibit patience and wait for the occurrence of a GC event or you can trigger one by clicking the ‘Cause GC’ button.

In case of the occurrence of a GC event, this tab would show detailed information about the type of objects your app is assigning, the number of allocated objects, and the amount of space these objects are consuming on the heap.

The fastest way to determine the actions that lead to any memory leaks you spot in Memory Monitor is to carry out various actions in your app; and stimulate a GC before and after you perform each action. You can then check each ‘Heap’ tab result for any unfamiliar allocations. The sooner you get something unusual, the better you will get the idea about which part of your app is responsible for the memory leakage.

If you’ve spent some time examining the ‘Heap’ results, and still don’t know about the reason causing your app’s memory issues, you need to try creating a snapshot of all the objects in your app’s heap (alias a HPROF file). The sole objective of this is to get more comprehensive information.

To create a heap dump, click the ‘Dump HPROF’ icon which appears next to the ‘Update Heap’ icon.

This will open a ‘Save HPROF File’ dialogue where you can name your HPROF file and set where it should be saved. After you save this file, you can open it as a new Android Studio project. Just choose ‘File’ from the Android Studio menu bar, and then click ‘Open.’ Now, in the new window, move to the space where you saved your HPROF, pick it, and then click ‘OK.’ Android Studio will open this file in a new ‘HPROF Viewer’ tab.

The most sought after way of using the HPROF Viewer to recognize objects might be responsible for memory leaks. So it is necessary to select each class name, then choose the occurrence of that class and examine the reference tree that appears in the bottom of this window.


If you want to build better apps, do make a habit of using advanced tools & techniques and check for your app’s memory related problems. If you are aware of any such tools, do share them in the below comment section.


Most Popular

To Top