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

查看:25
本文介绍了Android:Bitmap 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?

推荐答案

第一个位图不是 解码第二个时收集的垃圾.Garbage Collector 将在稍后决定时执行此操作.如果你想尽快释放内存,你应该在解码第二个位图之前调用 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:Bitmap recycle() 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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