查找浪费内存的类 [英] Find the class that waste the Memory

查看:89
本文介绍了查找浪费内存的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前,我在Google Play上发布了一个应用程序,我的大多数用户都对应用程序的内存使用存在严重问题,这是事实,因为当我从正在运行的应用程序"标签中检查内存使用情况时,我看到它需要 80〜110MB 的内存,如下图所示:

I've published an app several months ago in the Google Play, most of my user have strong problem with the memory usage of my app and that was true, because when I checked my memory usage from "Running Application" Tab, I see that it take 80~110MB of memory, like the below picture:

要查找导致此问题的类/活动或代码段,我发现MAT(内存分析工具)插件很有用,但确实让我感到困惑,请考虑以下图片:

To find the Class/Activity or pieces of the code that cause this problem I found the MAT (Memory Analysis Tools) plugin useful but it's really make me confused, please take into account to the following image:

已用内存的总大小为 9.8MB ,但是正在运行的应用程序"选项卡同时显示 80MB

The total size of the used memory is 9.8MB however "Running Application" Tab show me 80MB at the same time!

另一个问题是直方图,byte[]对象的浅堆过高.

The other problem is Histogram, the Shallow Heap of byte[] object is too high.

正常吗?另外,当我调试某些Google项目时,字节值总是过高!

Is it normal? Also when I debug some Google project, the byte value always is too high!

那我怎么才能找到所有使用这些内存的东西呢?

So how can I find what is using all this memory?

推荐答案

Android应用程序启动时,它将继承由Zygote分配的对象-一种使应用程序生成速度更快的技巧.因此,当您进行内存转储时,它们将显示为您自己的内存.

When an Android application starts, it inherits objects allocated by Zygote - a trick to make application spawning faster. So when you get a memory dump they show up as your own.

为什么Zygote有这么多内存分配的答案是另一个使resource加载更快的技巧.在Android启动过程中,Zygote 预加载一些类和资源列表,因此所有其他应用程序都不需要每次都加载它们.

The answer to why Zygote has that much memory allocation is another trick to make resource loading faster. During Android boot up, Zygote preloads some list of classes and resources so all other application doesn't need to load them every time.

请参见 ZygoteInit.java #330来实现此预加载.

See ZygoteInit.java#330 for implementation of this preloading.

字节数组的大量使用是由于这种预加载的资源(位图).

Heavy use of byte arrays are due to this preloaded resources (which are bitmaps).

请参阅此答案,以获取有关来自权威资源的MAT/字节数组使用情况分析的更具体示例.

See this answer for more concrete example of MAT / byte array usage analysis from a definitive resource.

这篇关于查找浪费内存的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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