How to treat memory leaks using the new AndroidProfiler
Asked Answered
A

1

11

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.

Arlenaarlene answered 26/11, 2017 at 22:37 Comment(3)
"Where, in the new tool, is, for example, this "Analyzer Tasks" and "Analysis Results" that shows instances that have leaks?" -- AFAIK, it doesn't exist. If you capture a heap dump from code via Debug.dumpHprofData(), IIRC, AS 3.0.1 will use the earlier tool that has those panes.Geriatrics
That "dumpHprofData" method should be used when OutOfMemoryError occurs? To see who is holding my activity where should I put this call?Arlenaarlene
"That "dumpHprofData" method should be used when OutOfMemoryError occurs?" -- probably not, as your environment will be fairly unstable then. "To see who is holding my activity where should I put this call?" -- well, LeakCanary should be telling you that. And it dumps the HPROF data using this call (and I think there's an option for getting its HPROF file, though I forget the details). Otherwise, you might call dumpHprofData() in an instrumentation test, after you think that you have set up a leak scenario.Geriatrics
S
9
  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

Schlieren answered 23/4, 2018 at 7:30 Comment(1)
Don't forge to add .hprof in the end of the file's name -while exporting the heap dump. Ex: test.hprofBoutis

© 2022 - 2024 — McMap. All rights reserved.