超出内存发生错误具有很高的堆大小,低分配的大小。为什么?大小、很高、发生错误、分配

2023-09-04 05:36:57 作者:闹够了,该成熟了

我有一个相当存储器密集的过程,可能会导致堆大小接近其极限(在此情况下,如24)生长。

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

然而,当该处理结束,并且已分配存储器(如在DDMS堆工具,并在堆转储列出)远低得多的像6或7

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:

尽管在DDMS的堆标签分配的内存和堆转储本身只显示内存为7MB分配有一个没有被垃圾收集,未提及隐记忆?如果是这样,我怎么跟踪下来?

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?

看来内存不足的错误都是基于堆的大小,而不是分配的内存大小。那么,有没有办法迫使堆大小来背下来时,它并不需要那么高的时候,内存密集型过程就结束了?

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?

感谢您的见解。

推荐答案

当你试图使用更多的内存比允许的最大堆大小时出现的内存不足。但是请注意,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.