Android和运行OOM与自由空间的剩余空间 [英] Android and running OOM with free heap space left

查看:170
本文介绍了Android和运行OOM与自由空间的剩余空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用大量的免费的堆空间免费的,可以对一只股票的Galaxy S3增长的一个应用程序运行的OOM。该应用程序运行在其他设备上的罚款。

I have an app running OOM with plenty of free heap space free and available to grow on a stock Galaxy S3. The app runs fine on other devices.

知道,在传统的Java,这可以通过作为永久代空间内OOM引起的,我试图寻找到的Dalvik如何处理这一点,但无法找到任何决定性的。机器人SDK似乎都缺少MemoryUsage中和ManagementFactory,这样,你会在Java中,我不能让他们

Knowing that in traditional Java this can be caused by being OOM within the permanent generation space, I tried looking into how Dalvik handles this, but couldn't find anything definitive. Androids SDK seems to be missing both MemoryUsage and ManagementFactory, so I can't get them as you would in Java.

我试图找出如果Android有一个永久代的空间,我可以检查它的内容,我怎么能得到的大小和自由,它的Dalvik处理什么进入这个空间比JVM等不同的。

I'm trying to find out if Android has a permanent generation space, can I inspect its contents, how can I get the size and free, does Dalvik handle what goes into this space different than JVM, etc.

也开到其他的想法,如果这个空间不存在,或者它是不可能的。

Also open to other ideas if this space doesn't exist or it's not likely.

在应用信息的位。运行OOM的S3运行4.1.2。该应用程序使用了大约12-25 MB的堆的最大堆大小可被周围45MBs。它有很多本地资源的图像,并延迟加载更多的后来。我.recylce()ING的位图。该应用程序大致相同点每次崩溃。我已经给了在周围崩溃当场code好好看看,我没有看到任何不寻常的。其他设备上运行该code就好了。

A bit of info on the app. The S3 that is running OOM is running 4.1.2. The app uses roughly 12-25 MBs of heap with the max heap size available being around 45MBs. It has a lot of local resource images, and lazy loads many more later on. I'm .recylce()ing the bitmaps. The app crashes in roughly the same spot every time. I've gave a good look at the code around the spot that crashes and I'm not seeing anything out of the ordinary. Other devices run this code just fine.

推荐答案

在我看来,你遇到了堆碎片问题。

It seems to me that you're experiencing a heap fragmentation issue.

您在有足够的可用空间堆未能在1.2MB分配。该系统可能无法找到1.2MB连续的空块,因为你的堆是完全分散,不能进一步扩大您的堆。

You are failing on a 1.2MB allocation in a heap that has plenty of free space. The system probably cannot find a continuous empty chunk of 1.2MB since your heap is completely fragmented, and it can't enlarge your heap further.

从理论上说,你不应该担心碎片问题和自动处理GC为你..

Theoretically, you're not supposed to worry about fragmentation issues and the automatic GC handles it for you..

在实践中,我见过不止一次,给予系统GC伸出援助之手,有时达到宏伟的结果。

Practically, I've seen more than once that giving the system GC a helping hand sometimes achieves magnificent results.

这里有一些想法来解决碎片问题:

Here are some ideas to work around fragmentation issues:


  1. 运行的System.gc()手动战略地位的位置。建议有样活性的onDestroy大对象的破坏过程。其他的地方是前大的分配,如位图。你去code任何大的位图前,添加手动GC。如果以后要减少地方选区的数量,通过查询各种堆大小功能,增加对有问题的内存条件下的手动测试。

  1. Run System.gc() manually in strategically placed locations. Popular locations are during destruction of large objects like activity onDestroy. Other place is before large allocations like Bitmaps. Before you decode any large bitmap, add a manual GC. If you want to reduce the number of GCs later, add an manual test for problematic memory conditions by querying the various heap size functions.

辅助系统GC免费的资源更快。这将prevent从第一名产生碎片的堆。材料的大量关于如何做到这一点的网。一些从我的头顶:

Assist the System GC free resources faster. This will prevent your heap from becoming fragmented in the first place. Plenty of material on the net on how to do that. Some from the top of my head:


  • 如果您有频繁的小分配,尽量采用内存重用方案就像在游泳池做对象池,和重用的对象,而不是分配新的。这种优化常见于Android的适配器(就像在列表视图,视图重用)

  • 手动设置变量为null,当你不需要他们为了从参考图中明确断开它们并将其标记,便于GC(同样适用于明确清除数据的集合)

  • 避免一成不变的类时,你有足够的操作,如字符串的链接,使用的StringBuilder ,而不是普通的字符串小号

  • 避免完全终结

  • 避免循环引用,或至少改变其中的一个弱引用

  • 需要它们的地方一般使用弱引用

  • If you have frequent small allocations, try to adopt a memory reuse scheme like making a pool of objects, and reusing objects in the pool instead of allocating new ones. This optimization is often seen in android adapters (like view reuse in listviews)
  • Manually set variables to null when you don't need them in order to explicitly disconnect them from the reference graph and mark them for easy GC (same goes for explicitly clearing data collections)
  • Avoid immutable classes when you have plenty of operations, like concatenation of strings, use StringBuilder instead of regular Strings
  • Avoid finalizers completely
  • Avoid circular references, or at least change one of them to a weak reference
  • Use weak references in general where they're needed

您可以衡量你是如何成功的通过确保当你只使用其中的一小部分的堆没有长到巨大的规模。

You can measure how successful you are by making sure your heap does not grow to huge sizes when you're only using a small part of it.

另一个想法是尽量使1.2MB分配较小。通过使用更小的图像或解码过程中重新调整他们。

Another idea is to try and make the 1.2MB allocation smaller.. By using smaller images or rescaling them during decoding.

这篇关于Android和运行OOM与自由空间的剩余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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