• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to diagnose OutOfMemoryError in Android  RSS feed

 
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • 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.


 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!