在android中回收位图
Android 中何时应该处理位图内存管理或回收位图?
例如,在android中创建位图的方法很少,如下所示
Bitmap.createBitmap
Bitmap.createScaledBitmap
BitmapFactory
但是当android为位图分配内存时,必须清除该内存,以便在将来的应用程序中我们不会遇到内存不足错误问题
When one should take care of Bitmap memory management or recycling bitmap in android ?
For example , there are few ways to create a bitmaps in android like following
Bitmap.createBitmap
Bitmap.createScaledBitmap
BitmapFactory
But when android allocate memory for bitmap which must be cleared so that in future application we won't face running out of memory error problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 3.0 之前的 Android 版本中,位图是在 VM 外部分配的。 Android 在 Bitmap 的 Finalize() 方法中取回该内存。您可以通过调用 Bitmap.recycle() 让 Android 更快地回收内存,而不是等待 GC 对它们调用 Finalize()。
只有当您创建并丢弃大量位图时,这才是真正的问题。也就是说,如果您分配内存的速度更快,那么 GC 可以清理留下的垃圾,此时您会收到 OutOfMemoryError。
在android 3.0及更高版本中,位图内存是在VM内部分配的,因此可以回收位图内存,而无需对其调用finalize()。
In Android versions prior to 3.0, the Bitmaps are allocated outside of the VM. Android gets back this memory in Bitmap's finalize() method. You can let Android reclaim the memory faster by calling Bitmap.recycle() instead of waiting on the GC to call finalize() on them.
This is only really a problem if you're creating and discarding a lot of the Bitmaps. That is, if you are allocating memory faster then the GC can clean up the garbage left behind, at which point you get an OutOfMemoryError.
In android 3.0 and later, the Bitmap memory is allocated inside of the VM, so Bitmap memory can be reclaimed without having to call finalize() on them.
当您不需要位图时,您可以回收它。例如
,PS 位图内存的行为因设备而异,我的意思是
设备之间的行为不同
例如,在 S4 上,位图大小可能会超出 VM 预算(增长堆),最高可达 240MB
经个人测试确认
,并且不会导致OutOfMemoryError
,但在某些其他设备中如果位图大小超过(增长堆)最多 16MB,则可能会导致OutOfMemoryError
,它因设备而异,因为有些设备具有较大的堆,而有些则没有。相信我,处理不断增长的堆并不是一件容易的事。其他建议是在清单内的
application
标记中使用android:largeHeap="true"
。`
you recycle the bitmap whenever you don't need it. for example
P.S bitmaps memory are acting different from each devices, what i mean is
is different from device to another
for example Bitmap size can exceed VM budget(Growing Heap) up to 240MB on the S4
tested and confirmed by personal testing
and it doesn't causeOutOfMemoryError
but in some other devices if the bitmap size exceed (Growing Heap) up to 16MB it may causeOutOfMemoryError
its quite different from device to another because some devices has large heap while some not. and trust me dealing with Growing Heap is not easy task.additional advice is to use
android:largeHeap="true"
in yourapplication
tag inside of the manifest.`