0

我的应用程序中有一个内存泄漏,我派生到 AsyncTask 类和 onPostExecute 方法中。onPostExcecute 方法接收 9 个按比例缩小的位图并将它们放入滚动视图中。滚动视图又被放入警报对话框中。

有什么建议可以阻止这种内存泄漏吗?我试图对所有 9 个位图进行弱引用,但没有成功。

cahcing 是另一种选择吗?

protected void onPostExecute(Bitmap[] bitmap) {

if (view == null) { 
    view = factory.inflate(R.layout.alertviews4, null);
    alertadd = new AlertDialog.Builder(context);
}



WeakReference <Bitmap> weakBitmap0 = new WeakReference <Bitmap> (bitmap[0]);
WeakReference <Bitmap> weakBitmap1 = new WeakReference <Bitmap> (bitmap[1]);
WeakReference <Bitmap> weakBitmap2 = new WeakReference <Bitmap> (bitmap[2]);
WeakReference <Bitmap> weakBitmap3 = new WeakReference <Bitmap> (bitmap[3]);
WeakReference <Bitmap> weakBitmap4 = new WeakReference <Bitmap> (bitmap[4]);
WeakReference <Bitmap> weakBitmap5 = new WeakReference <Bitmap> (bitmap[5]);
WeakReference <Bitmap> weakBitmap6 = new WeakReference <Bitmap> (bitmap[6]);
WeakReference <Bitmap> weakBitmap7 = new WeakReference <Bitmap> (bitmap[7]);
WeakReference <Bitmap> weakBitmap8 = new WeakReference <Bitmap> (bitmap[8]);

mImage = (ImageView) view.findViewById(R.id.img1);
mImage.setImageBitmap(weakBitmap0.get());
mImage = (ImageView) view.findViewById(R.id.img2);
mImage.setImageBitmap(weakBitmap1.get());
mImage = (ImageView) view.findViewById(R.id.img3);
mImage.setImageBitmap(weakBitmap2.get());
mImage = (ImageView) view.findViewById(R.id.img4);
mImage.setImageBitmap(weakBitmap3.get());
mImage = (ImageView) view.findViewById(R.id.img5);
mImage.setImageBitmap(weakBitmap4.get());
mImage = (ImageView) view.findViewById(R.id.img6);
mImage.setImageBitmap(weakBitmap5.get());
mImage = (ImageView) view.findViewById(R.id.img7);
mImage.setImageBitmap(weakBitmap6.get());
mImage = (ImageView) view.findViewById(R.id.img8);
mImage.setImageBitmap(weakBitmap7.get());
mImage = (ImageView) view.findViewById(R.id.img9);
mImage.setImageBitmap(weakBitmap8.get());

alertadd.setView(view);

alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dlg, int sumthin) {

    }
});
alertadd.show();

}

4

1 回答 1

0

对于 API 级别 < 11,位图是一个困难的话题。它们被计入应用程序的堆限制,但不只是由 GC 收集。最佳实践是调用 bitmap.recycle() 并使指针无效以允许 GC 收集引用。

于 2013-07-04T22:02:21.607 回答