Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to treat memory leaks using the new AndroidProfiler

I'm trying to learn how to identify and treat memory leaks in my App. I'm reading this great article, but I'm a bit confused about how to apply that in the new "Android Profiler" tool, that came with Android Studio 3.0.

I'have a test application and LeakCanary is pointing that my App has memory leaks.

In fact, when I run the profiler and press "dump java heap" I can see that there are 4 instances of my MyCollectionActivity and, when I click on these instances, I can see the details:

enter image description here

But the tool that is shown in the article has options I cannot see in the "Android Profiler":

I will not go into depth about how to navigate the huge memory heap. Instead I’ll direct your attention to the Analyzer Tasks in the upper right corner of the screenshot below. All you have to do to detect the memory leak introduced in the example above is to check Detect Leaked Activities and then press play to get the leaked activity to show up under Analysis Results.

Where, in the new tool, is, for example, this "Analyzer Tasks" and "Analysis Results" that shows instances that have leaks?

enter image description here

If we select the leaked activity we are presented with a Reference Tree where the reference that is keeping the activity alive can be identified. By looking for instances with depth zero we find that the instance mListener located within the location manager is the reason our activity can’t be garbage collected.

That Android Profiler's image doesn't show that Reference Tree too, it shows the reference, but it doesn't seem to be very useful.

like image 281
alexpfx Avatar asked Nov 26 '17 22:11

alexpfx


1 Answers

  1. First, you need to save the Heap Dump. The dump save with extension hprof. Check the below-attached image for saving this file.
  2. Open the same file in Android Studio, after that, you will be able to see the Analyser Tasks option at the top right corner.

enter image description here

like image 65
Shanki Bansal Avatar answered Oct 14 '22 21:10

Shanki Bansal