Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Diagnose OutOfMemoryError in Android

DZone's Guide to

How to Diagnose OutOfMemoryError in Android

The easy technique in this tutorial will help you find memory leaks to troubleshoot OutOfMemoryError in your Android apps.

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

Diagnosing OutOfMemoryError in Android apps can become a tricky, tedious job. Here, we would like to show you an easy technique to troubleshoot OutOfMemoryError in Android apps.

BuggyApp

To facilitate this exercise, we built a simple android application which would trigger OutOfMemoryError. We named this app "BuggyApp." Do you like the name? This app has just one page, which contains only one button: “Add Objects”. When you click on this button, the app starts to add a very large string of objects to an array list in an infinite loop. When a large string of objects is infinitely added to an array list, it will result in OutOfMemoryError.

OutOfMemoryErrorApp-1

OutOfMemoryErrorApp-2

Android App experiencing OutOfMemoryError.

A few seconds after you click on the “Add Objects” button, the application will crash with an OutOfMemoryError, as shown below:

OutOfMemoryErrorStackTrace

Android OutOfMemoryError crash stack trace.

How to Diagnose OutOfMemoryError

Now, let’s get to the interesting part – how to diagnose OutOfMemoryError. It’s just two simple steps, my friend:

  1. Capture Android Heap Dumps
  2. Analyze Android Heap Dumps

1. Capture Android Heap Dumps

The first step is to capture heap dumps from the Android app. A Heap Dump is a snapshot of memory, which contains information about the objects in the memory, their references, and their size. Here is an article which summarizes 3 different options to capture heap dumps from an Android app. You can use any one of the options that is convenient for you to capture heap dumps. We used option #2 mentioned in the article to capture the heap dump from this "BuggyApp."

2. Analyze Android Heap Dumps

The second step is to analyze the captured heap dump. To analyze Android heap dumps, we used the free online tool HeapHero. This tool analyzes Android heap dumps and points out potential memory leak suspects. Besides identifying memory leaks, HeapHero also identifies the amount of memory wasted due to poor programming practices and recommends solutions to fix them. Since it’s an online tool, you don’t have to do any downloading, installation, or setup. All you need to do is upload the heap dump file that was captured in step 1 to HeapHero.

We uploaded the heap dump file captured in step 1 to HeapHero. HeapHero generated this beautiful report. In the report, there is a section called "Large Objects." This section reports all the large objects that are residing in the memory.

large-objects

From the report, you can see that the android.view.inputmethod.InputMethodManager.sInstance object is occupying 96.7% of overall memory. This is a significant size for any object. In fact, HeapHero has built-in intelligence to identify potential memory leaking objects. Based on this intelligence, HeapHero also marked ‘android.view.inputmethod.InputMethodManager.sInstance’ as a potential memory leak suspect.

When we clicked on the ‘android.view.inputmethod.InputMethodManager.sInstance’ hyperlink in the report, it started to show the stack trace/code path of the leaking objects.

code-path

You can see the ‘buggycompany.com.buggyapp.MainActivity’ object holding the ‘java.util.ArrayList’ object, which, in turn, is holding onto a large amount of string objects (96.6%). Aha! The exact lines of code that we wrote in ‘BuggyApp’ to trigger OutOfMemoryError.

That’s it, my friend. With these two simple steps, you might be able to solve complex OutOfMemoryError and memory leak problems. We hope this article will help you to isolate the exact lines of code that are triggering memory problems in your app.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:
android ,performance ,mobile ,tutorial ,memory

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}