android防止内存溢出浅析(一)

本文详细介绍了在Android游戏开发中如何应对Bitmap引起的内存溢出问题,通过及时销毁、设置采样率及巧妙运用软引用三种方案有效解决内存管理难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做android游戏开发有一段时间了,对于开发中遇到额OutOfMemory 异常真的是很头疼,今天就在此总结下。

游戏开发中遇到的内存溢出基本上都是出现在加载Bitmap的时候 ,原因是Bitmap实在是太占内存了,尤其是对于高分辨率的的图片一定要小心使用了。

下面就罗列出三点解决使用Bitmap时出现的内存溢出问题的方案:

一.及时的销毁:

虽然,系统能够确认Bitmap分配的内存最终会被销毁,但是由于它占用的内存过多,所以很可能会超过java堆的限制。因此,在用完Bitmap时,要及时的recycle掉。recycle并不能确定立即就会将Bitmap释放掉,但是会给虚拟机一个暗示:“该图片可以释放了”。

二. 设置一定的采样率:

有时候,我们要显示的区域很小,没有必要将整个图片都加载出来,而只需要记载一个缩小过的图片,这时候可以设置一定的采样率,那么就可以大大减小占用的内存。如下面的代码:

private ImageView preview;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri),
null, options);
preview.setImageBitmap(bitmap);

三、巧妙的运用软引用(SoftRefrence)
有些时候,我们使用Bitmap后没有保留对它的引用,因此就无法调用Recycle函数。这时候巧妙的运用软引用,可以使Bitmap在内存快不足时得到有效的释放。如下例:

private class MyAdapter extends BaseAdapter {

private ArrayList mBitmapRefs = new ArrayList();
private ArrayList mValues;
private Context mContext;
private LayoutInflater mInflater;

MyAdapter(Context context, ArrayList values) {
mContext = context;
mValues = values;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return mValues.size();
}

public Object getItem(int i) {
return mValues.get(i);
}

public long getItemId(int i) {
return i;
}

public View getView(int i, View view, ViewGroup viewGroup) {
View newView = null;
if(view != null) {
newView = view;
} else {
newView =(View)mInflater.inflate(R.layout.image_view, false);
}

Bitmap bitmap = BitmapFactory.decodeFile(mValues.get(i).fileName);
mBitmapRefs.add(new SoftReference(bitmap)); //此处加入ArrayList
((ImageView)newView).setImageBitmap(bitmap);

return newView;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值