是不是需要调用Bitmap.recycle()之后使用(在Android的)? [英] Is it needed to call Bitmap.recycle() after used (in Android)?

查看:457
本文介绍了是不是需要调用Bitmap.recycle()之后使用(在Android的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据Android的参考文件Bitmap.recycle的():

According to Android Reference Document of Bitmap.recycle():

释放与此相关的位图的原生对象,并清除引用的像素数据。这不会释放的像素数据同步;它简单地允许它被垃圾,如果有任何其它引用收集。位图被标记为死亡,这意味着它会抛出一个异常的getPixels()或的setPixels()被调用,并且将绘制什么。这种操作是不可逆的,所以它应该只有当你确定没有其它用途的位图调用。 这是一种先进的呼叫,并且通常不需要叫,因为正常的GC进程将释放该内存的时候有这种位图没有更多的参考。

Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

不过,许多书我读通过调用Bitmap.recycle(建议可用内存),一旦确定不再需要它了。

But, many books I read suggest to free memory by calling Bitmap.recycle() once make sure no longer need it.

这让我迷惑:是否需要调用Bitmap.recycle()使用后

It make me confused: Is it needed to call Bitmap.recycle() after used?

推荐答案

这要看情况。

如果您在Android 3.0及以上运行你的应用程序,它是不需要的,因为GC将照顾它完美。

If you run your app on Android 3.0 and above, it's not needed as the GC will take care of it perfectly.

不过,如果您在旧版本上运行你的应用程序,因为位图无法通过GC监测井(它认为自己是一个参考的大小),你可以得到OOM,如在谷歌IO演讲<一个href="http://www.google.com/events/io/2011/sessions/memory-management-for-android-apps.html">here.

However, if you run your app on older versions, since bitmaps don't get monitored well by the GC (it thinks they are the size of a reference), you could get OOM, as shown on Google IO lecture here.

在任何情况下,它仍然建议打电话,只要你确定你不需要位图再回收。它甚至新的Andr​​oid版本好,因为它降低了所需的自动内存管理方面的工作......

In any case, it's still recommended to call recycle as soon as you are sure you don't need the bitmap anymore. It's good even for new android versions, since it lowers the work needed for automatic memory management...

其实,我记得我问过类似的问题<一href="http://stackoverflow.com/questions/11954435/what-does-calling-bitmap-recycle-on-api-11-do">here.

In fact, I remember I've asked a similar question here.

另外,如果你需要的位图使用JNI额外的控制,检查<一href="http://stackoverflow.com/questions/18250951/jni-bitmap-operations-for-helping-to-avoid-oom">this交

Also, if you need extra control of bitmaps using JNI, check out this post.

因此​​,简而言之,答案是,它不再需要,但仍建议。

So, in short, the answer is that it's not needed anymore, but still recommended.

这篇关于是不是需要调用Bitmap.recycle()之后使用(在Android的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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