How to treat memory leaks using the new AndroidProfiler

浪尽此生 提交于 2019-12-09 11:08:26

问题


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:

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?

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.


回答1:


  1. First you need to save the Heap Dumb. 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.



来源:https://stackoverflow.com/questions/47501739/how-to-treat-memory-leaks-using-the-new-androidprofiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!