Android:位图recycle()如何工作? [英] Android: How does Bitmap recycle() work?

查看:81
本文介绍了Android:位图recycle()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已将图像加载到类似位图的对象中

Let's say I have loaded an image in a bitmap object like

Bitmap myBitmap = BitmapFactory.decodeFile(myFile);

现在,如果我加载另一个位图,会发生什么情况

Now, what will happen if I load another bitmap like

myBitmap = BitmapFactory.decodeFile(myFile2);

第一个myBitmap发生了什么?它是否已收集垃圾,还是必须在加载另一个位图之前手动进行垃圾收集,例如. myBitmap.recycle()?

What happens to the first myBitmap? Does it get Garbage Collected or do I have to manually garbage collect it before loading another bitmap, eg. myBitmap.recycle()?

还有,有没有更好的方法可以加载大图像并在循环使用的过程中一个接一个地显示它们?

Also, is there a better way to load large images and display them one after another while recycling on the way?

推荐答案

第一个位图不是解码第二个垃圾时. 垃圾收集器将在以后决定时再做.如果要尽快释放内存,则应在解码第二个位图之前调用recycle().

The first bitmap is not garbage collected when you decode the second one. Garbage Collector will do it later whenever it decides. If you want to free memory ASAP you should call recycle() just before decoding the second bitmap.

如果要加载非常大的图像,则应重新采样.这是一个示例:在将图像加载到位图时出现内存不足问题对象.

If you want to load really big image you should resample it. Here's an example: Strange out of memory issue while loading an image to a Bitmap object.

这篇关于Android:位图recycle()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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