位图,Bitmap.recycle(),在WeakReferences和垃圾收集 [英] Bitmap, Bitmap.recycle(), WeakReferences, and Garbage Collection

查看:521
本文介绍了位图,Bitmap.recycle(),在WeakReferences和垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AFAIK在Android上,建议参考,以避免内存泄漏的位图对象为在WeakReferences。当不再有硬引用保持一个位图对象,垃圾收集器会自动收集它。

AFAIK on Android, it is recommended to reference Bitmap objects as WeakReferences in order to avoid memory leaks. When no more hard references are kept of a bitmap object, the garbage collector will automatically collect it.

现在,如果我理解正确的话,该方法Bitmap.recycle()必须被调用,以释放一个位图。我想这是因为位图对象有特殊的内存管理。

Now, if I understand correctly, the method Bitmap.recycle() must always be called to free a Bitmap. I think this is because Bitmap objects have special memory management.

这是否正确?

如果这是真的,在WeakReferences使用时,必须有内存泄漏,因为Bitmap.recycle()不会被调用了在WeakReferences被释放的时候。或者说,在某种程度上,是在WeakReferences足以避免内存泄漏?

If this is true, when using WeakReferences, there must be memory leaks because Bitmap.recycle() is never called when the WeakReferences are freed. Or, somehow, are WeakReferences sufficient to avoid memory leaks?

感谢

推荐答案

Bitamp.recycle不是要求被称为,因为垃圾收集器将清理自己最终的位图(长因为没有参考文献)。在Android的位图在本机内存中创建,而不是在VM堆,所以对VM堆实际的位图对象是非常小的,因为它不包含任何实际的位图数据。 (编辑:不再情况下的Andr​​oid 3.0 +的)位图的实际尺寸仍会对你堆的使用计数的目的GC,并确保您的应用程序不使用太多内存

Bitamp.recycle isn't required to be called, as the garbage collector will clean up bitmaps on its own eventually (as long as there are no references). Bitmaps in Android are created in native memory, not on the VM heap, so the actual Bitmap object on the VM heap is very small as it doesn't contain any actual bitmap data. ( no longer the case as of Android 3.0+) The real size of the bitmap will still be counted against your heap usage for purposes of GC and making sure your app doesn't use too much memory.

不过,GC似乎有点喜怒无常,当谈到位图。如果你只是删除所有硬引用,它有时会(对我来说)挂到了位图的时候不多了,也许是因为不可思议的方式Bitmap对象分配/计数。 Bitmap.recycle似乎是很好的得到GC更快地收集对象。

However, the GC seems to be a little moody when it comes to Bitmaps. If you just remove all hard references, it would sometimes (in my case) hang onto the Bitmaps for a little while longer, perhaps because of the weird way Bitmap objects are allocated/counted. Bitmap.recycle seems to be good for getting the GC to collect that object more quickly.

无论哪种方式,你不会的泄漏内存,如果你不叫Bitmap.recycle只要你不保持硬引用意外。您可能会遇到OutOfMemoryError异常,如果您尝试分配过多位图一次或过大的位图,而无需调用.recycle,虽然。

Either way, you won't leak memory if you don't call Bitmap.recycle as long as you don't keep hard references accidentally. You may encounter OutOfMemoryErrors if you try to allocate too many bitmaps at once or too large bitmaps without calling .recycle, though.

编辑:要注意,作为机器人3.0的,位图不再分配在本机存储器是很重要的。在分配上的VM堆像任何其他Java对象。不过,我说过不需要调用循环仍然适用。

It is important to note that as of Android 3.0, Bitmaps are no longer allocated in native memory. The are allocated on the VM heap like any other Java object. However, what I said about not needing to call recycle still applies.

这篇关于位图,Bitmap.recycle(),在WeakReferences和垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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