Android 中的堆转储和内存使用差异?
好吧,我通常不会寻求帮助,因为我通常更喜欢自己寻找答案,但不幸的是我似乎无法做到这一点。
我正在摆弄 android 2.1+ 的动态壁纸,一切进展顺利,直到我开始收到 OOM 错误:
Java.lang.OutOfMemoryError: bitmap size exceeds VM budget
好的 - 我做了我的研究,发现我肯定在某个地方存在内存泄漏。所以我阅读了如何分析此类问题,打开 DDMS 并进行了堆转储。堆大小通常约为 4.5-5mb,并且永远不会超过 60%。这是没有意义的,因为我检查了我的开发手机上正在运行的服务,它报告我的进程使用了 35-42mb 的内存。
我并不是要求任何人找到我的泄漏,因为这需要大量代码等。只是为了解释如何正确分析我的内存使用情况,因为我似乎对发生的事情完全傻眼了。
Alright, I am not typically one to ask for help as I usually prefer to find answers on my own, but unfortunately I seem unable to do that.
I am messing around with live wallpapers for android 2.1+ Things went pretty well until a ways in I started getting a OOM error:
Java.lang.OutOfMemoryError: bitmap size exceeds VM budget
Ok - I did my research and found out that I surely have a memory leak somewhere. So I read up on how to analyze such problems, opened up the DDMS and did a Heap dump. the Heap size usually runs around 4.5-5mb in size and never more than 60% full. This didn't make sense, because I checked the running services on my development phone, and it reported my process as using anywhere from 35-42mb of memory.
I'm not asking for anyone to find my Leak as that would require lots of code etc. Just to explain how to properly analyze my memory usage, since I seem to be utterly dumbfounded as to what is going on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你已经这样做了,但以防万一......你检查过触发 OutOfMemoryError 的行吗?您尝试创建的位图有多大?
如果是泄漏,这里有一篇文章可以帮助您分析堆转储(尽管有文章标题,您不必使用 Eclipse):
Android ==>内存分析==> Eclipse 内存分析器?
祝你好运。
I'm guessing you already did this, but just in case... did you examine the line that triggered the OutOfMemoryError? How big is the bitmap you are trying to create?
If it is a leak here is an article that might help you analyze a heap dump (you won't have to use eclipse despite the article title):
Android ==> Memory Analysing ==> Eclipse memory analyzer?
Good Luck.
谢谢 satur9nine,在发布这篇文章后,我实际上做了更多的挖掘,终于了解了发生了什么(当然那是在我发布问题之后,去弄清楚)。
我发现android服务管理器中报告的内存使用情况被称为本机堆。这是堆大小+位图(可能还有其他东西?)。但我仍然有点困惑为什么它有时会超过 40 MB,而我加载的图像只有 ~900 KB。当它点击时,图像在加载到内存中时不再保持压缩状态。
因此,我加载大约 30 个小 PNG 占用了大量内存。
我循环遍历这些 PNG 来制作雨动画。我当时并没有意识到这是一个糟糕的策略。一旦我意识到这是问题所在,我就放弃了它,并为自己编写了一个简单的小“粒子系统”,以生成一个漂亮的下雨动画,仅将一个粒子图标加载到内存中!
Thanks satur9nine, I actually did some more digging after posting this and finally got a grasp of what was going on (of course that was after I posted the question, go figure).
I discovered that the memory usage reported in the services manager in android is referred to as the Native Heap. This is the Heap size + bitmaps (among other things possibly?). But I was still a bit confused as to why it was 40+ MB at times, when the images I were loading were only ~900 KB. When it clicked that images no longer stay compressed when loaded in to memory.
So my loading of approximately 30 small PNG's was taking up a massive amount of memory.
I was cycling through each of these PNG's to make a Rain animation. I didn't realize at the time that was a bad strategy. Once I realized that was the problem, I scrapped it and Wrote myself a simple little "Particle System" To generate a nice rain animation with just one particle icon loaded in to memory!