差异的B / W bitmap.recycle()和位图= NULL位图、差异、recycle、bitmap

2023-09-13 01:52:41 作者:幸福轻的太沉重°

我是在一个情况下我使用了一个for循环,下载一组图片,我把它转换成位图图像。因此,为了避免内存不足的错误,我想回收位图。但不幸的是,我遇到了另一个异常说像查看试图用一个循环位图

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".

但还是允许我用位= NULL 为null使用位图。所以我的问题是,让我的位图帮助我释放所使用的内存吗?或者我应该提供 bitmap.recycle()在我的code其他部分?

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?

推荐答案

调用循环()指示您在使用该资源完成的系统,而该系统可现在是免费的,它是使用了非托管内存。一旦你已经配置在这样的资源,它的行为通常是不确定的(人会合理地期望它只是不再工作)。

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:

您不会有过时的对象的引用,当您尝试使用它们这是行不通的 在垃圾收集器将知道要清理位图对象的托管方,释放更多的内存