我有一个内存相当密集的进程,可能会导致堆大小增长到接近其极限(在本例中为 24)。
然而,当该进程结束时,分配的内存(如 DDMS 堆工具和堆转储中所列)要低得多,如 6 或 7。
尽管分配的内存较低,并且有大量可用内存,堆似乎没有回来。
因此,即使它说有足够的可用内存,在这种情况下我仍然会遇到内存不足错误。
因此,它引出了几个问题:
-
即使 DDMS 的“堆”选项卡中的已分配内存和堆转储本身仅显示已分配的 7MB 内存,是否存在未提及的未被垃圾收集的隐藏内存?如果是这样,我该如何追踪这个问题?
-
内存不足错误似乎是基于堆大小,而不是分配的内存大小。那么,当内存密集型进程结束时堆大小不需要那么高时,有没有办法强制堆大小回落?
感谢您的见解。
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:
-
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?
Thanks for your insights.
发布评论
评论(3)
当您尝试使用超过允许的最大堆大小的内存时,就会发生内存不足。但请注意,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.
您是否尝试过弹出 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/
对于 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.