Bitmap、Bitmap.recycle()、WeakReferences 和垃圾回收

Bitmap, Bitmap.recycle(), WeakReferences, and Garbage Collection(Bitmap、Bitmap.recycle()、WeakReferences 和垃圾回收)

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

问题描述

AFAIK 在 Android 上,建议将 Bitmap 对象引用为 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() 方法来释放位图.我认为这是因为 Bitmap 对象具有特殊的内存管理.

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.

对吗?

如果这是真的,当使用 Wea​​kReferences 时,肯定会发生内存泄漏,因为当 WeakReferences 被释放时,Bitmap.recycle() 永远不会被调用.或者,不知何故,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?

谢谢

推荐答案

Bitmap.recycle 不是必需调用的,因为垃圾收集器会清理位图最终独立(只要没有参考).Android 中的位图是在本机内存中创建的,而不是在 VM 堆上创建的,因此 VM 堆上的实际位图对象非常小,因为它不包含任何实际的位图数据.(Android 3.0+ 不再是这种情况) 位图的实际大小仍将计入您的堆使用量,以用于 GC 并确保您的应用程序不会使用太多内存.

Bitmap.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.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,就不会泄漏内存.但是,如果您尝试一次分配太多位图或未调用 recycle 就分配了太大位图,您可能会遇到 OutOfMemoryErrors.

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.

需要注意的是,从 Android 3.0 开始,位图不再分配在本机内存中.它们像任何其他 Java 对象一样分配在 VM 堆上.但是,我所说的不需要调用回收仍然适用.

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、Bitmap.recycle()、WeakReferences 和垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Bitmap、Bitmap.recycle()、WeakReferences 和垃圾回收

基础教程推荐