diff b/w bitmap.recycle() 和 bitmap=null [英] Diff b/w bitmap.recycle() and bitmap=null

查看:23
本文介绍了diff b/w bitmap.recycle() 和 bitmap=null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 for 循环下载一组图像并将其转换为位图图像.所以为了避免 OutOfMemory 错误,我试图回收位图.但不幸的是,我遇到了另一个异常,比如 查看尝试使用回收位图".

I am in a situation where I have used a for loop to download a set of images and I am converting it into bitmap images. So in order to avoid OutOfMemory error, I am trying to recycle the bitmaps. But unfortunately I am running into another exception saying something like "View trying to use a recycled bitmap".

但我仍然可以使用 bitmap=null 将使用过的位图归零.所以我的问题是,让我的位图 null 帮助我释放已用的内存吗?或者我应该在代码的其他部分提供 bitmap.recycle() 吗?

But still I am allowed to null the used bitmap by using bitmap=null. So my question is, will making my bitmap null help me in freeing up the used memory? or should I provide bitmap.recycle() in some other part of my code?

推荐答案

调用 recycle() 向系统表明您已使用完该资源,并且系统现在可以释放非托管内存它正在使用.一旦您以这种方式处理了资源,它的行为通常是未定义的(人们可以合理地期望它不再起作用).

Calling recycle() indicates to the system that you are finished using that resource and that the system may now free the unmanaged memory that it was using. Once you have disposed of a resource in this way, its behaviour is usually undefined (one would reasonably expect it to simply no longer work).

之后将引用设置为 null 有两个好处:

Setting the reference to null afterwards has two benefits:

  • 当您尝试使用它们时,不会有对无效对象的过时引用
  • 垃圾收集器将知道清理位图对象的托管端,释放更多内存

这篇关于diff b/w bitmap.recycle() 和 bitmap=null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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