Dalvik 外部存储器是什么?图片分配问题/WeakHashMap问题
我正在尝试编写一个图像缓存。我使用 WeakHashMap 来保存下载的图像并设置在列表 ImageView 中显示它们。然而,过了一段时间我遇到了:
02-17 15:13:00.416: D/dalvikvm(11766): GC_FOR_MALLOC freed 0K, 69% free 3447K/10823K, external 21870K/22699K, paused 16ms
02-17 15:13:00.455: D/dalvikvm(11766): GC_EXTERNAL_ALLOC freed <1K, 69% free 3447K/10823K, external 21870K/22699K, paused 25ms
02-17 15:13:00.463: E/dalvikvm-heap(11766): 116160-byte external allocation too large for this process.
02-17 15:13:00.490: E/GraphicsJNI(11766): VM won't let us allocate 116160 bytes
之后我收到了 OutOfMemoryError 错误。
显然我的“外部记忆”已经用完了,无论那是什么。有大量可用的堆,但这似乎并不重要。 GC 似乎无法清理这个“外部内存”,尽管我唯一的参考是列表中的弱哈希和 imageViews(应该只是一个实例,重用)...
有人可以帮忙吗我对此事有一些了解吗?我用完的外部存储器是什么?为什么 GC 不清理旧映像?
编辑:我找到了导致内存无法释放的原因:WeakHashmap 在不再引用 KEYS 时释放条目,而不是在不再引用 VALUES 时释放条目。我的主要问题仍然是:外部存储器是什么?
I'm trying to write an Image cache. I'm using a WeakHashMap to hold the downloaded images and set show them in a list ImageView. However, after a while I run into:
02-17 15:13:00.416: D/dalvikvm(11766): GC_FOR_MALLOC freed 0K, 69% free 3447K/10823K, external 21870K/22699K, paused 16ms
02-17 15:13:00.455: D/dalvikvm(11766): GC_EXTERNAL_ALLOC freed <1K, 69% free 3447K/10823K, external 21870K/22699K, paused 25ms
02-17 15:13:00.463: E/dalvikvm-heap(11766): 116160-byte external allocation too large for this process.
02-17 15:13:00.490: E/GraphicsJNI(11766): VM won't let us allocate 116160 bytes
After which I am treated with a OutOfMemoryError.
Obviousely I'm running out of "external memory" whatever that is. There is plenty of heap available but that doesn't seem to matter. The GC does not seem to be able to clean up this "external memory" although the only references I have are the weak hash and the imageViews in the list (which should only be a single instance, reused)...
Can somebody please help me shed some light on this matter? What is this external memory I'm running out of? Why isn't the GC cleaning up the old images?
Edit: I since found out what caused my memory to not be released: WeakHashmap releases entries when the KEYS are no longer referenced, not when the VALUES are not referenced. My main question still remains: what is that external memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信基本的 android 有 16 MB 堆内存,它用于所有程序的对象,包括你的哈希图,所以没有太多内存可供你使用。某些具有定制 ROM 的个别设备可能有更多空间,但默认情况下为 16 MB。它实际上是在 Android 本身中编码的,特别是在文件
frameworks/base/core/jni/AndroidRuntime.cpp
中:您可以使用
recycle()
方法和其他一些方法技术可以最大限度地减少应用程序的内存占用,但如果您需要存储超过 16 MB 的数据,那么您就很不走运了。I believe base android has 16 MB heap memory, which is used for all of program's objects, including your hashmap, so not much memory for you to use. Some individual devices with customised ROM may have more space, but by default is 16 MB. It's actually coded in the Android itself, specifically in file
frameworks/base/core/jni/AndroidRuntime.cpp
:You may be able to use
recycle()
method and some other techniques to minimise your app's memory footprint, but if you need to store more than 16 MB of data, then you're pretty much out of luck.您遇到的问题是您试图分配比可用内存更多的内存。即使您有 60% 的空闲空间,如果您尝试分配堆大小 61% 的内容,您也会收到内存不足错误。
The problem you're having is that you're trying to allocate more memory than is available. Even if you have 60% free, if you try to allocate something 61% the size of heap, you'll get a n Out of Memory Error.