如何使用新的AndroidProfiler处理内存泄漏 [英] How to treat memory leaks using the new AndroidProfiler

查看:432
本文介绍了如何使用新的AndroidProfiler处理内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何识别和处理App中的内存泄漏.我正在阅读很棒的文章,但是我对如何在Android Studio 3.0随附的新"Android Profiler"工具中应用它感到有些困惑.

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.

我有一个测试应用程序,LeakCanary指出我的应用程序存在内存泄漏.

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

实际上,当我运行探查器并按转储Java堆"时,我可以看到MyCollectionActivity有4个实例,当我单击这些实例时,可以看到详细信息:

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:

但是本文中显示的工具具有我无法在"Android Profiler"中看到的选项:

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?

如果我们选择泄漏的活动,则会为您提供参考 可以使活动保持活动状态的引用所在的树 确定.通过查找深度为零的实例,我们发现 位于位置管理器中的mListener实例是原因 我们的活动无法被垃圾收集.

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.

Android Profiler的图像也没有显示参考树,它显示了参考,但是它似乎不是很有用.

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. 首先,您需要保存 堆转储.转储文件的扩展名为 hprof .检查下面的图像以保存此文件.
  2. 在Android Studio中打开相同的文件,然后,您将能够在右上角看到 Analyser Tasks (分析器任务)选项.
  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.

这篇关于如何使用新的AndroidProfiler处理内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆