DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Does the OCP Exam Still Make Sense?
  • Multi-Stream Joins With SQL
  • DevOps Pipeline and Its Essential Tools
  • You’ve Got Mail… and It’s a SPAM!
  1. DZone
  2. Coding
  3. Languages
  4. How to Diagnose OutOfMemoryError in Android

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.

Ram Lakshmanan user avatar by
Ram Lakshmanan
CORE ·
Jun. 20, 18 · Tutorial
Like (1)
Save
Tweet
Share
4.76K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Android (robot) Dump (program) app garbage collection Memory (storage engine) Object (computer science)

Opinions expressed by DZone contributors are their own.

Trending

  • Does the OCP Exam Still Make Sense?
  • Multi-Stream Joins With SQL
  • DevOps Pipeline and Its Essential Tools
  • You’ve Got Mail… and It’s a SPAM!

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: