Android 中的内存分析工具?
我只是想知道如何在 android 中使用内存分析器工具。请告诉我如何执行此操作以了解特定项目的内存泄漏。有人可以给我一步一步的程序吗?
I just want to know how to work with Memory analyzer tool in android. Please tell me how to do this to know about memory leaks for a particular project. Can someone give me step wise procedure for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
就这样。您将看到可能泄漏的列表,但其中一些是误报的。我建议您多次运行要检查泄漏的活动,然后再次运行 MAT。
That's all. You'll se a list of possible leaks, but some of them are false-positive. I recommend you to run an activity you want to check leaks in several times and then run MAT again.
尝试使用内存分析器工具 (MAT)
查看这些文章:
另外看看这些SO帖子:
Try Using the Memory Analyzer Tool (MAT)
Look at these articles:
Also look at these SO Posts:
当您未使用 android.permission.WRITE_EXTERNAL_STORAGE 时,写入 hprof 会失败。作为一种特殊情况,当您在设备上进行测试时,请检查您的 SD 卡是否以仅充电模式连接,否则您将无法对 SD 卡进行写访问
Write to hprof fails when you have not used android.permission.WRITE_EXTERNAL_STORAGE. As a special case when you are testing on device, check if your SD card is connected in Charge ONLY mode otherwise you wont have write access on SD Card
安装MAT。
转到工具->Android ->Android 设备监视器(DDMS)
在左上角设备,选择项目名称并
单击更新堆。
然后单击转储HPROF文件并将其保存在桌面中。
然后运行命令提示符并指出 Android-sdk 目录。
示例: hprof-conv 位于 E 目录中。所以运行
命令为
cd android/sdk/platform-tools
。然后最后通过引用此 doc 来运行此命令来转换此命令文件格式以在 MAT 中读取。
E:\Android\sdk\platform-tools>
hprof-conv "C:\Users\Steve\Desktop\yourfilename.hprof"
"C:\Users\Steve\Desktop\leakage.hprof"
现在您的文件将转换为leakage.hprof。检查此文件并
在 MAT 中打开它。
编辑:单击详细信息,然后它将显示类名称。在其中
左键单击 -> Path To GC Roots ->包含所有引用
。然后它显示类名。Install MAT.
Go to Tools->Android ->Android device monitor(DDMS)
In the top left side under Devices,select the project name and
click Update Heap.
Then Click Dump HPROF File and saved it in desktop.
Then run the Command prompt and point out the Android-sdk directory.
For an Example: hprof-conv is located in E directory. So run the
command as
cd android/sdk/platform-tools
.Then finally run this command by referring this doc to convert this file format to read it in MAT.
E:\Android\sdk\platform-tools>
hprof-conv "C:\Users\Steve\Desktop\yourfilename.hprof"
"C:\Users\Steve\Desktop\leakage.hprof"
Now your file will converted to leakage.hprof.check this file and
open it in MAT.
Edit: Click details,then it will show the class names.In that
Left Click -> Path To GC Roots ->With all references
.Then it shows the class name.Square开源了一个内存分析工具:LeakCanary。
LeakCanary 的核心是 MAT。
Square open sourced a memory analysis tool: LeakCanary.
The core of the LeakCanary is MAT.
我建议您查看Leak Canary。它本身不是内存分析器,而是泄漏检测器。只需使用您的应用程序,打开和关闭活动,然后让库完成它的工作。它甚至会告诉您泄漏发生的位置。泄漏发生后,只需给泄漏分析仪一些时间来完成工作 - 通常需要大约 2 分钟或更长时间才能找到泄漏源。
I'd recommend taking a look at Leak Canary. It's not a memory analyzer per se, but more a leak detector. Just use your app, open and close activities and let the library do it's job. It will even tell you about where the leak occured. Just give the leak analyzer some time to do its work after the leak occured - it usually takes around 2 minutes or more until the source of the leak has been found.
请更新到 Android studio 版本
1.4
或1.5
。在此版本中,Android 支持了名为
Analyzer Task
的新工具这对于避免内存泄漏非常有用。
Please update to Android studio version
1.4
or1.5
.In this version, android has supported new tool its called
Analyzer Task
It is very useful to avoid memory leak.