Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Memory Leak Class/Activity in Android

I am New to this Android platfrom. In one application, there is a memory leak. But i couldn't able to find in which actiity or class the memory leak is happening.

I am using MAT to get the infirmation that the memory leak is there or not. But not able to find which activity is leaking.

Please tell me how to find the which activity is leaking.

Thanks.

like image 437
user388269 Avatar asked Sep 15 '10 03:09

user388269


People also ask

How do I find memory leaks on Android?

The Memory Profiler is a component in the Android Profiler that helps you identify memory leaks and memory churn that can lead to stutter, freezes, and even app crashes. It shows a realtime graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.

What causes memory leak Android?

A memory leak can easily occur in Android when AsyncTasks, Handlers, Singletons, Threads, and other components are used incorrectly. I'll use threads, singletons, and listeners to demonstrate how we can cause leaks and how to avoid and fix them. Check out my Github repository. I've got some code examples for you.

How do you find memory leaks inside apps?

If you suspect you are running into a temporal leak, a good way to check is to use Android Studio's memory profiler. Once you start a session within the profiler, take the steps to reproduce the leak, but wait for a longer period of time before dumping the heap and inspecting. The leak may be gone after the extra time.


2 Answers

Even if this question is quiet old. I think a small tutorial with images might be helpful. So first you need to install the MAT plugin. After a restart click on Dump HPROF file:

Device controls

After some time (about 30 seconds) you'll get a dialog where you can click on finish. Than you get this view:

Dump result

There you left click on the biggest part and filter for with outgoing references. Then you get this view here:

Here you can see that I missed to delete items in my ContentManager where several items were stored in my pending HashMap.

like image 125
rekire Avatar answered Sep 22 '22 14:09

rekire


The best post I found yet on tracking memory leaks on android platforms is:

http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html

The first part is outdated because it's now integrated into Eclipse but most of the post is great!

like image 27
ol_v_er Avatar answered Sep 19 '22 14:09

ol_v_er