• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to diagnose OutOfMemoryError in Android

 
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


    Fig: Android App that simulates OutOfMemoryError


        Fig: Android App experiencing OutOfMemoryError

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


                       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.


                       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.


                     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.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic