如何在 Android 中查找内存泄漏类/活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3714394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:30:03  来源:igfitidea点击:

How to find Memory Leak Class/Activity in Android

androidmemory-leaks

提问by user388269

I am New to this Android platfrom. In one application, there is a memory leak. But i couldn't able to find in which actiity or class the memory leak is happening.

我是这个 Android 平台的新手。在一个应用程序中,存在内存泄漏。但是我无法找到发生内存泄漏的活动或类。

I am using MAT to get the infirmation that the memory leak is there or not. But not able to find which activity is leaking.

我正在使用 MAT 来确认内存泄漏是否存在。但无法找到哪个活动正在泄漏。

Please tell me how to find the which activity is leaking.

请告诉我如何找到泄漏的活动。

Thanks.

谢谢。

回答by rekire

Even if this question is quiet old. I think a small tutorial with images might be helpful. So first you need to install the MAT plugin. After a restart click on Dump HPROF file:

即使这个问题是安静的老。我认为带有图像的小教程可能会有所帮助。所以首先你需要安装MAT 插件。重新启动后单击转储 HPROF 文件

Device controls

设备控制

After some time (about 30 seconds) you'll get a dialog where you can click on finish. Than you get this view:

一段时间后(大约 30 秒),您将看到一个对话框,您可以在其中单击完成。比你得到这个观点:

Dump result

转储结果

There you left click on the biggest part and filter for with outgoing references. Then you get this view here:

在那里您左键单击最大的部分并使用传出参考进行过滤。然后你会在这里看到这个视图:

Here you can see that I missed to delete items in my ContentManager where several items were stored in my pendingHashMap.

在这里您可以看到我错过了删除 ContentManager 中的项目,其中有几个项目存储在我的pending 中HashMap

回答by ol_v_er

The best post I found yet on tracking memory leaks on android platforms is:

我发现的关于在 android 平台上跟踪内存泄漏的最佳帖子是:

http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html

http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html

The first part is outdated because it's now integrated into Eclipse but most of the post is great!

第一部分已经过时,因为它现在已集成到 Eclipse 中,但大部分帖子都很棒!

回答by Malder

Use LeakCanary- a memory leak detection library for Android and Java. LeakCanary will automatically show a notification when an activity memory leak is detected in your debug build. Installing this library is very simple.

使用LeakCanary- 适用于 Android 和 Java 的内存泄漏检测库。在调试版本中检测到活动内存泄漏时,LeakCanary 将自动显示通知。安装这个库非常简单。

https://github.com/square/leakcanary

https://github.com/square/leakcanary

Notification example:

通知示例:

enter image description here

在此处输入图片说明

回答by jelford

I can't comment on the previous post, but it's important to realise that you absolutely should worry about memory management, especially on the resource-confined environment that is the Android mobile platform. For more on that, check here:

我无法对之前的帖子发表评论,但重要的是要意识到您绝对应该担心内存管理,尤其是在资源受限的 Android 移动平台环境中。有关更多信息,请查看此处:

http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

回答by Jana

You can see it in the DDMS.

您可以在 DDMS 中看到它。

Simply select your app package name and click update heap the above button.

只需选择您的应用程序包名称,然后单击上面的按钮更新堆。

Now goto Heap Tab click Cause GC.

现在转到 Heap Tab 单击 Cause GC。

you will find the listing of objects used.

您将找到所用对象的列表。

回答by Phil

You can analysis android memory leaks automatically with MMAT,MMAT github page

你可以用MMAT自动分析android内存泄漏,MMAT github页面