HOW TO DIAGNOSE OUTOFMEMORYERROR IN ANDROID?

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 would start to add very large string objects to an array list in an infinite while loop. When large string objects are infinitely added to an array list, it will result in OutOfMemoryError.

OutOfMemoryErrorApp-1

Fig: Android App that simulates OutOfMemoryError

OutOfMemoryErrorApp-2

Fig: Android App experiencing OutOfMemoryError

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

OutOfMemoryErrorStackTrace

Fig: 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 easy simple steps, my friend:

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

1. Capture Android Heap Dumps

First step is to capture heap dumps from the android app. Heap Dump is a snapshot of memory, which contains information about the objects in the memory, their references, their size… Here is an article which summarizes 3 different options to capture heap dumps from the 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

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 the same. Since it’s an online tool, you don’t have to do any downloading, installation, setup…. All you need to do is to upload the heap dump file that was captured in step #1 to HeapHero.

Now we uploaded the heap dump file captured in step #1 to HeapHero. HeapHero generated this beautiful report. In the report, there is a section: ‘Large Objects’. This section reports all the large objects that are residing in the memory.

large-objects

Fig: Large Objects sitting in memory reported by HeapHero

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

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

code-path

Fig: Code Path of memory leaking objects

You could see the ‘buggycompany.com.buggyapp.MainActivity’ object holding ‘java.util.ArrayList’ object, which in turn is holding on to large amount of string objects (96.6%). Ahaha, 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: