内存泄漏和外的存储器(memory-leak and out-of-memory)

2019-10-30 10:25发布

它真的令人沮丧 - 我在我的应用程序索姆样的内存泄漏,尽管我回收的位图和使用garbagecollector。 在我的应用我有一个ListView和当一个klicks一个子项目几个图像加载到一个alerdialogbuilder并在滚动视图。 我没有超过9个图像滚动视图和一段时间后,我得到了内存不足的异常。 我能做什么比回收位图吗? 我应该创建imageviews例如一个WeakReference的,所以garbagecollector可以照顾他们?

我把一切都放在一个扩展的AsyncTask一类,并在DO-的背景,方法的另一方法被调用,以使图像efficently缩小。 这些图像随后被发送到在那里它们被放入滚动视图的方法onPostExecute(位图[]位图)。 在哪里泄漏? 我的银河S3的堆大小为64 MB,并装载有9图像后的MemoryUsage更是飙升到可能42 MB,则下一个克利克47 MB​​ ......再没有什么奇怪happends ......我想,好吧..好它停止在这里47 MB​​ ......但经过10多个点击内存使用量继续攀升至55也许MB ..等等直到应用程序崩溃。

我已经通过设置大型堆=在manifest资源配置文件真正的暂时解决了这个问题。 但我不喜欢推迟的问题!

我也无法理解为什么的MemoryUsage是9个图像如此之大。 他们被缩小前的图像的平均大小约250KB。

非常感激的帮助! :-)

    class BitmapWorkerTask extends AsyncTask <Integer, Void, Bitmap[]> {

        private int[] data;
        private int[] width, height;
        private int nmbrOfImages;
        private String[] scrollText;
        private ImageView mImage;
        private View view;
        private LayoutInflater factory;
        private AlertDialog.Builder alertadd;

        public BitmapWorkerTask(int[] width, int[] height, int nmbrOfImages, String[] scrollText) {

            this.width = width;
            this.height = height;
            this.nmbrOfImages = nmbrOfImages;
            this.scrollText = scrollText;

            mImage = null;
            view = null;
            factory = null;
            alertadd = null;
            System.gc();

            try {
                for (int i = 0; i < scaledBitmap.length; i++) {
                    scaledBitmap[i].recycle();
                    System.out.println("i: " + i);
                }
            } catch (NullPointerException ne) {
                System.out.println("nullpointerexception ... gick inte recycla bitmapbilder");
            }


            switch (nmbrOfImages) {
            case 0:
                data = new int[1];

                break;
            case 1:
                data = new int[3];

                break;
            case 2:
                data = new int[5];

                break;
            case 3:
                data = new int[9];

                break;
            }


        }

        @Override
        protected Bitmap[] doInBackground(Integer ... params) {

            switch (nmbrOfImages) {
            case 0:
                data[0] = params[0];
                break;
            case 1:
                data[0] = params[0];
                data[1] = params[1];
                data[2] = params[2];
                break;
            case 2:
                data[0] = params[0];
                data[1] = params[1];
                data[2] = params[2];
                data[3] = params[3];
                data[4] = params[4];
                break;
            case 3:
                data[0] = params[0];
                data[1] = params[1];
                data[2] = params[2];
                data[3] = params[3];
                data[4] = params[4];
                data[5] = params[5];
                data[6] = params[6];
                data[7] = params[7];
                data[8] = params[8];
                break;
            }

            alertadd = new AlertDialog.Builder(context);
            factory = LayoutInflater.from(context);

            return decodeSampledBitmapFromResource(context.getResources(), data, width, height);
        }

        protected void onPostExecute(Bitmap[] bitmap) { 
            switch (nmbrOfImages) {
                case 0:
                    if (view == null) { 
                        view = factory.inflate(R.layout.alertviews, null);
                    }
                    ImageView mImage = (ImageView) view.findViewById(R.id.extra_img);
                    mImage.setImageBitmap(bitmap[0]);
                    alertadd.setView(view);
                    alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dlg, int sumthin) {

                        }
                    });
                    alertadd.show();
                break;
                case 1:

                    if (view == null) { 
                        view = factory.inflate(R.layout.alertviews2, null);
                    }

                    mImage = (ImageView) view.findViewById(R.id.img1);
                    mImage.setImageBitmap(bitmap[0]);
                    mImage = (ImageView) view.findViewById(R.id.img2);
                    mImage.setImageBitmap(bitmap[1]);
                    mImage = (ImageView) view.findViewById(R.id.img3);
                    mImage.setImageBitmap(bitmap[2]);

                    try {
                        TextView mText2 = (TextView) view.findViewById(R.id.text_img1_scrollview);
                        mText2.setText(scrollText[0]);
                        mText2 = (TextView) view.findViewById(R.id.text_img2_scrollview);
                        mText2.setText(scrollText[1]);
                        mText2 = (TextView) view.findViewById(R.id.text_img3_scrollview);
                        mText2.setText(scrollText[2]);
                    } catch (NullPointerException ne) {
                        System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
                    }
                    alertadd.setView(view);
                    alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dlg, int sumthin) {

                        }
                    });
                    alertadd.show();

                break;
                case 2:

                    if (view == null) { 
                        view = factory.inflate(R.layout.alertviews3, null);
                    }
                    mImage = (ImageView) view.findViewById(R.id.img1);
                    mImage.setImageBitmap(bitmap[0]);
                    mImage = (ImageView) view.findViewById(R.id.img2);
                    mImage.setImageBitmap(bitmap[1]);
                    mImage = (ImageView) view.findViewById(R.id.img3);
                    mImage.setImageBitmap(bitmap[2]);
                    mImage = (ImageView) view.findViewById(R.id.img4);
                    mImage.setImageBitmap(bitmap[3]);
                    mImage = (ImageView) view.findViewById(R.id.img5);
                    mImage.setImageBitmap(bitmap[4]);

                    try {
                        TextView mText3 = (TextView) view.findViewById(R.id.text_img1_scrollview);
                        mText3.setText(scrollText[0]);
                        mText3 = (TextView) view.findViewById(R.id.text_img2_scrollview);
                        mText3.setText(scrollText[1]);
                        mText3 = (TextView) view.findViewById(R.id.text_img3_scrollview);
                        mText3.setText(scrollText[2]);
                        mText3 = (TextView) view.findViewById(R.id.text_img4_scrollview);
                        mText3.setText(scrollText[3]);
                        mText3 = (TextView) view.findViewById(R.id.text_img5_scrollview);
                        mText3.setText(scrollText[4]);
                    } catch (NullPointerException ne) {
                        System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
                    }
                    alertadd.setView(view);
                    alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dlg, int sumthin) {

                        }
                    });
                    alertadd.show();

                break;
                case 3:

                if (view == null) { 
                    view = factory.inflate(R.layout.alertviews4, null);
                }

                AlertDialog.Builder alertadd = new AlertDialog.Builder(context);

                mImage = (ImageView) view.findViewById(R.id.img1);
                mImage.setImageBitmap(bitmap[0]);
                    mImage = (ImageView) view.findViewById(R.id.img2);
                    mImage.setImageBitmap(bitmap[1]);
                    mImage = (ImageView) view.findViewById(R.id.img3);
                    mImage.setImageBitmap(bitmap[2]);
                    mImage = (ImageView) view.findViewById(R.id.img4);
                    mImage.setImageBitmap(bitmap[3]);
                    mImage = (ImageView) view.findViewById(R.id.img5);
                    mImage.setImageBitmap(bitmap[4]);
                    mImage = (ImageView) view.findViewById(R.id.img6);
                    mImage.setImageBitmap(bitmap[5]);
                    mImage = (ImageView) view.findViewById(R.id.img7);
                    mImage.setImageBitmap(bitmap[6]);
                    mImage = (ImageView) view.findViewById(R.id.img8);
                    mImage.setImageBitmap(bitmap[7]);
                    mImage = (ImageView) view.findViewById(R.id.img9);
                    mImage.setImageBitmap(bitmap[8]);

                    try {
                        TextView mText4 = (TextView) view.findViewById(R.id.text_img1_scrollview);
                        mText4.setText(scrollText[0]);
                        mText4 = (TextView) view.findViewById(R.id.text_img2_scrollview);
                        mText4.setText(scrollText[1]);
                        mText4 = (TextView) view.findViewById(R.id.text_img3_scrollview);
                        mText4.setText(scrollText[2]);
                        mText4 = (TextView) view.findViewById(R.id.text_img4_scrollview);
                        mText4.setText(scrollText[3]);
                        mText4 = (TextView) view.findViewById(R.id.text_img5_scrollview);
                        mText4.setText(scrollText[4]);
                        mText4 = (TextView) view.findViewById(R.id.text_img6_scrollview);
                        mText4.setText(scrollText[5]);
                        mText4 = (TextView) view.findViewById(R.id.text_img7_scrollview);
                        mText4.setText(scrollText[6]);
                        mText4 = (TextView) view.findViewById(R.id.text_img8_scrollview);
                        mText4.setText(scrollText[7]);
                        mText4 = (TextView) view.findViewById(R.id.text_img9_scrollview);
                        mText4.setText(scrollText[8]);
                    } catch (NullPointerException ne) {
                        System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
                    }

                alertadd.setView(view);

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

                    public void onClick(DialogInterface dlg, int sumthin) {

                    }
                });
                alertadd.show();

            break;
            }
        }



    }
文章来源: memory-leak and out-of-memory