Android位图图像视图内存泄漏



我将4x4 imageView放在一个活动(BoardActivity)中,用户可以通过单击来更改图像。使用HTC Desire(Android 2.2.2),我在大约30分钟的密集使用中获得了OOM(内存不足)-EDIT:此活动的第16次启动,但没有其他设备产生此问题(Android 2.1和Android 2.2.1)。有可能吗,我在位图/图像视图使用中犯了一些错误,导致了此错误?首先,我将所有资源ID加载到一个映射中:

private Map<String, Integer> imageResourceMap;
imageResourceMap = new HashMap<String, Integer>();
imageResourceMap.put("a", R.drawable.a);
imageResourceMap.put("b", R.drawable.b);
imageResourceMap.put("c", R.drawable.c);
//... I store 55 drawable's resourceId in this map

然后我调整每个图像的大小并将其保存到位图中,用Map:表示

private static Map<String, Bitmap> imageBitmap;
private void loadBitmaps(int imagesSize) {
for (String s : imageResourceMap.keySet()) {
Bitmap tmp = getBitmapFromRes(imageResourceMap.get(s), imagesSize);
imageBitmap.put(s, tmp);
}
}
private Bitmap getBitmapFromRes(int resId, int imageSize) {
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
InputStream fis = getResources().openRawResource(resId);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > imageSize || o.outWidth > imageSize) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(imageSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = getResources().openRawResource(resId);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}

我将imageViews保存在一个数组中,并使用以下函数初始化所有图像:

private static ImageView[][] imageViews;
private ImageView getImage(String name) {
MyImageView item = new MyImageView(this, i, j, c + "");
item.setImageBitmap(imageBitmap.get(name));
item.setAdjustViewBounds(true);
return item;
}

当我需要更改图像时,我只需更改其资源:

imageViews[i][j].setImageBitmap(imageBitmap.get("a"));

就在我完成活动之前,我回收位图:

private void recycleImageBitmaps() {
for (Bitmap b : imageBitmap.values()) {
if (b != null) {
b.recycle();
}
}
}

在AndroidManifest.xml中,我将此活动声明为"singleTask":

<activity android:name=".game.board.BoardActivity" android:launchMode="singleTask">
</activity>

在这个应用程序(游戏)中,我们多次重新打开此活动。。。我做错了什么?这会导致内存不足错误吗?

更正更正getBitmapFromRes如下:

private Bitmap getBitmapFromRes(int resId, int imageSize) {
Bitmap tmp = null;
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
InputStream fis = getResources().openRawResource(resId);
tmp = BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > imageSize || o.outWidth > imageSize) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(imageSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = getResources().openRawResource(resId);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(tmp != null){
tmp.recycle();
tmp = null;
}
}
return b;
}

HTC仍然在第11次活动开始时崩溃。

编辑:此活动(BoardActivity)从具有4个imageButton的活动(MenuActivity)启动,并且位于Tabhost活动中。imageButtons声明如下:

<ImageButton
android:id="@+id/game_menu_CreateButton"
android:layout_width="120dip" 
android:layout_height="120dip"
android:layout_alignRight="@+id/textView1"
android:layout_alignTop="@+id/textView1"
android:background="@drawable/create" 
android:layout_marginRight="1sp"
android:layout_marginTop="10sp"
/>

当我从MenuActivity启动BoardActivity时,我不会在MenuActivity调用finish(),当我在BoardActivity调用finish()时,我也不会启动新的意图,所以它只是返回到已经打开的MenuActivity。在第16轮比赛中,我得了OOM。

为了减少内存,您可以尝试以下方法:

  • 将可绘制对象转换为位图后,可以将imageResourceMap设置为null。这将卸载3个抽屉
  • 避免存储对imageViews的引用。您可能正在存储imageViews,即使它们已从UI中删除
  • 更频繁地回收位图。不只是onDestroy,只要你知道一个位图没有被使用,你就可以回收它

编辑:根据评论中的对话:BitmapFactory.decodeStream返回的位图(fis,null,o)未分配给任何变量,因此不会回收。安卓2.2和2.3将在这一行有漏洞。

最新更新