How to Diagnose an 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.


Figure 1: Android App that simulates OutOfMemoryError


Figure 2: Android App experiencing OutOfMemoryError

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


Figure 3: 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 p 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.


Figure 4: 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.


Figure 5: 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%). Ah ha, 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.

Author bio: Ram Lakshmanan

ram-lakshshman

Every single day, millions & millions of people in North America — bank, travel, and commerce — use the applications that Ram Lakshmanan has architected. Ram is an acclaimed speaker in major conferences on scalability, availability, and performance topics. Recently, he has founded a startup, which specializes in troubleshooting performance problems.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job