How to Fix Memory Leak in Android Apps Using DDMS Tool

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 10, 2024

How to Test and Fix Memory Leak for Android Applications:

Memory optimization is a bigger challenge for any developer before pushing an app to the play store.

Performance of an app is equally important as its functionality. This article is based on the Heap analysis of an application through DDMS (Dalvik Debug Monitor Service).

Most of the Android applications run on JAVA and have threading issues as well.

Memory Leak In Android Application

Important Note – Dalvik Debug Monitor Server (DDMS) tool is available in Android Studio 3.0 and lower versions only. If you are using Studio 3.0 and above, make sure to use Android Profiler to profile your app’s CPU, memory, and network usage.

This tutorial will help you to understand and optimize memory leak for an android application using the DDMS tool.  Analyzing the memory will give an idea of memory and optimizing the memory values will thereby result in less memory leak.

Commonly used Terms

Enlisted below are some of the common terms that we will be using in this project:

  • Heap – It is the Runtime memory that is available for allocation and is distributed dynamically. It is platform dependent.
  • Dalvik heap – Dalvik heap is a combination of classes and data by zygote.
  • Zygote – Zygote helps to launch app processes when the system loads.
  • PSS – It is the count of pages within the memory for a process, and is measured in KB.
  • Garbage Collection – Garbage collection is tracking down all the objects that are still used and marks rest as Garbage.

DDMS (Dalvik Debug Monitor Server)

This is the tool that helps to analyze Runtime performance Monitoring. It also helps in profiling and tuning of applications.

Main Services offer by DDMS

  • Total Heap object allocations stats
  • App thread statics
  • Device Screen capture
  • Device File explorer
  • Incoming call and SMS inspection
  • Location data inspection
  • Network Static Console
  • Logcat (Verbose,Debug,Assert,Error)

Memory leak: 

An Application holds an Object for a long time even after serving the purpose and this object is not collected by GC.

Detection of Memory Leak: 

Generally, Android application shows a Dialog Pop up for an App which is not responding or in the worst case out of memory exception. Every Device has limited heap size and when an application tries for additional memory, Exceptions are observed.

In order to Check the Total heap size of an Android device, we can use the below commands:

  • cat /proc/meminfo | grep MemTotal
  • adb –d pull /system/build.prop

Steps to Launch DDMS Via ADT

Step #1: Launch adt bundle (easily available here) Or Install ADT bundle for Windows configuration using the below links.

windows 32: https://dl.google.com/android/adt/adt-bundle-windows-x86-20140702.zip

windows 64: https://dl.google.com/android/adt/adt-bundle-windows-x86_64-20140702.zip

Step #2: Click on SDK

SDK

Step #3: Click on tools

Tools

Step #4: Click on monitor.bat (DDMS)

monitor.bat (DDMS)

Note: For heap analysis, debug apk is required.

After successful installation of the ADT bundle, go to the command prompt (set path, if adb is not recognized as an internal or external command).

Check for the device connected to a system through adb command (adb devices). Once a device is connected, it is connected to the DDMS screen with the package name of an application.

Steps to Launch DDMS via Android Studio

Studio -> Tools -> Android -> Android device Monitor -> Click on DDMS button from the Menu.

Steps to Analysis of a heap

  • Select the package name of an app that is appearing on the DDMS screen.
  • Select the update heap icon.
  • Tap on cause GC before taking values for every action.
  • Now on the command prompt run command – adb shell dumpsys meminfo <package name> and press enter.
  • Values will appear on the command prompt.
  • PSS total, native, dalvik, heap size, heap free, heap-allocated are the values that will help the developers to debug the memory crashes.
  • Better representation of data will make the work easy for a developer.
  • Data should be like:
    • Objects value after clicking on cause GC.
    • PSS total
    • Native heap- heap size
    • Dalvik heap- heap size
    • Total of heap size
    • Native heap- heap alloc
    • Dalvik heap- heap alloc
    • Total of heap alloc
    • Native heap- heap free
    • Dalvik heap- heap free
    • Total of heap free
    • Objects (after clicking on cause GC from DDMS)
    • % used % free

Ideal testing: For better app analysis, try to perform different scenarios by keeping an app in the background, and by landing on the same page,  perform a complete flow of these commonly used scenarios.

Try to use lower-end devices, as the most common crash is observed because of memory that is Out Of Memory exception (OOM).

DDMS Screen

DDMS Screen

Samples of Dalvik Values

Samples of Dalvik Values

Conclusion

This article can be used to test heap analysis using the DDMS tool. Correct values for the parameters will help the Android developers to optimize memory.

Knowledge of testing tools is good but using those tools in an efficient way is considered to be an Art.

Happy Learning!!

Was this helpful?

Thanks for your feedback!

Leave a Comment