2013-07-04 81 views
0

我的應用程序中有一個memoryleak,我導出到AsyncTask類和onPostExecute方法中。 onPostExcecute方法接收9個縮小位圖並將其放入滾動視圖中。滾動視圖反過來放入alertdialog。儘管使用weakreference,asynctask中的memoryleak

任何建議如何阻止這種內存泄漏?我試圖讓所有9個位圖的弱引用沒有成功。

是否正在打開替代方案?

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(); 

}

回答

0

對於API級別< 11,位圖是一個很困難的話題。它們被計算到應用程序的堆棧限制,但不是簡單地由GC收集。最佳做法是調用bitmap.recycle()並使指針無效,以允許GC收集引用。

+0

自從它用作資源後,我無法在onPostExcecute方法中回收本地位圖變量。有什麼建議麼? –