AsyncTecute方法中的内存泄漏AsyncTask

时间:2013-07-10 13:50:29

标签: android memory-leaks android-asynctask weak-references

我的应用程序中有一个内存泄漏,根据Eclipse中的调试程序,它似乎位于Asynctask中的onPostExecute方法中。

它完全解码位图并将那些(9)放入滚动视图中。 scrollView又被放入AlertDialog中。 (AlertDialog.Builder)。这是在onPostExecute方法中完成的。

我认为我已经做了所有可以做的事情。

  • 扩展Asynctask的类是一个静态内部类。
  • 对延伸Asynctask的类进行了弱推理。
  • 有效地解码位图(即将那些位图缩小以适应实际的屏幕尺寸,并在需要时回收它们)。
  • 对asynctask中的每个对象进行了弱引用。

我在这里发布代码。我错过了至关重要的事吗?为什么弱推论根本不起作用?每次将这9个位图加载到scrollView中 - 大约。将1000个新对象添加到对象的总数中。同时堆也在增长,但是慢慢地。

    private WeakReference <ImageView> mImageV;
    private WeakReference <Bitmap[]> bitmapV;
    private WeakReference <AlertDialog.Builder> alertaddV;
    private WeakReference <View> viewV;


protected void onPostExecute(Bitmap[] bitmap) { 

                    if (view == null) { 
                        view = factory.inflate(R.layout.alertviews4, null);
                        viewV = new WeakReference <View> (view);
                    }


                    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) viewV.get().findViewById(R.id.img1);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap0.get());

                    mImage = (ImageView) viewV.get().findViewById(R.id.img2);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap1.get());

                    mImage = (ImageView) viewV.get().findViewById(R.id.img3);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap2.get());

                    mImage = (ImageView) viewV.get().findViewById(R.id.img4);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap3.get());

                    mImage = (ImageView) viewV.get().findViewById(R.id.img5);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap4.get());

                    mImage = (ImageView) viewV.get().findViewById(R.id.img6);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap5.get());

                    mImage = (ImageView) viewV.get().findViewById(R.id.img7);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap6.get());

                    mImage = (ImageView) viewV.get().findViewById(R.id.img8);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap7.get());

                    mImage = (ImageView) viewV.get().findViewById(R.id.img9);
                    mImageV = new WeakReference <ImageView> (mImage);
                    mImageV.get().setImageBitmap(weakBitmap8.get());

                    mImageV.get().getDrawable().setCallback(null);

                    alertaddV.get().setView(viewV.get());

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

                    public void onClick(DialogInterface dlg, int sumthin) {

                    }
                });
                alertaddV.get().show();

            }

1 个答案:

答案 0 :(得分:0)

我似乎在这方面取得了成功 - 这就是将onPostExecute-method中的代码解除为doInBackground-method。我提出的代码是:

  • 对xml文件进行充气。
  • findViewById。