堆大小较高但分配大小较低时会发生内存不足错误。为什么?

发布于 2024-11-02 23:27:50 字数 415 浏览 1 评论 0 原文

我有一个内存相当密集的进程,可能会导致堆大小增长到接近其极限(在本例中为 24)。

然而,当该进程结束时,分配的内存(如 DDMS 堆工具和堆转储中所列)要低得多,如 6 或 7。

尽管分配的内存较低,并且有大量可用内存,堆似乎没有回来。

因此,即使它说有足够的可用内存,在这种情况下我仍然会遇到内存不足错误。

因此,它引出了几个问题:

  1. 即使 DDMS 的“堆”选项卡中的已分配内存和堆转储本身仅显示已分配的 7MB 内存,是否存在未提及的未被垃圾收集的隐藏内存?如果是这样,我该如何追踪这个问题?

  2. 内存不足错误似乎是基于堆大小,而不是分配的内存大小。那么,当内存密集型进程结束时堆大小不需要那么高时,有没有办法强制堆大小回落?

感谢您的见解。

I have a fairly memory intensive process that can cause the heap size to grow near its limits (like 24 in this case).

However, when that process ends, and the Allocated memory (as listed in the DDMS Heap tool and in a heap dump) is far far lower like 6 or 7.

Despite the Allocated memory being low, and there being a lot of free memory, the heap doesn't seem to come back down.

So even though it says there is plenty of memory available I can still get out of memory errors in this case.

So it begs a couple questions:

  1. Even though the Allocated memory in the Heap tab of DDMS and the heap dumps themselves only show 7mb of memory as allocated is there hidden memory that is not mentioned that is not being garbage collected? If so, how do I track this down?

  2. It seems out of memory errors are based on heap size, not allocated memory size. So is there a way to force the heap size to come back down when it doesn't need to be so high when the memory intensive process is over?

Thanks for your insights.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北斗星光 2024-11-09 23:27:50

当您尝试使用超过允许的最大堆大小的内存时,就会发生内存不足。但请注意,Android 的 GC 目前不会对堆进行碎片整理,这似乎是您遇到的问题。您的堆可能有大量可用内存,但碎片化为小块。如果虚拟机找不到足够大的块来进行您尝试的分配,则会发生 OOME。

An out of memory occurs when you are trying to use more memory than the maximum allowed heap size. Note however that Android's GC currently does not defragment the heap and it seems that's the issue you are running into. Your heap may have a lot of memory available, but fragmented in small chunks. If the VM cannot find a chunk big enough for the allocation are you trying to do, an OOME will happen.

花心好男孩 2024-11-09 23:27:50

您是否尝试过弹出 System.gc() ?

如果您正在使用位图,您可能需要查看以下内容:http://davidjhinson.wordpress.com/2010/05/19/scarce-commodities-google-android-memory-and-bitmaps/

Have your tried popping in a System.gc() ?

If you're working with bitmaps, you might want to look at this: http://davidjhinson.wordpress.com/2010/05/19/scarce-commodities-google-android-memory-and-bitmaps/

风透绣罗衣 2024-11-09 23:27:50

对于 Android 3.2 (API 13) 及更高版本,如果您有需要布局端口和布局中的“match_parent”的大型位图,将 android:configChanges="orientation|screenSize" 添加到您的清单中可能会有所帮助-land xml 文件。

For Android 3.2 (API 13) and up, it may help to add android:configChanges="orientation|screenSize" to your manifest if you have large bitmaps that need "match_parent" from layout-port and layout-land xml files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文