0

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

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

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

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

我在这里发布代码。我错过了什么重要的事情吗?为什么弱引用根本不起作用?每次将这 9 个位图加载到滚动视图中 - 大约。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();

            }
4

1 回答 1

0

看来我在这方面取得了成功 - 将代码从 onPostExecute-method 提升到 doInBackground-method。我取出的代码是:

  • 膨胀 xml 文件。
  • 查找ViewById。
于 2013-07-10T18:39:54.073 回答