He is really disappointed - I have some kind of memory leak in my application, despite the fact that I process bitmap images and use garbagecollector. In my application, I have a list, and when one klicks in a sub-element, several images are loaded in alerdialogbuilder and in scrollview. I have no more than 9 images in scrollview, and after a while I get an exception in memory. What can I do more than process bitmaps? Should I create a weak link to images, for example, so that a garbagecollector can take care of them?
I put everything in a class that extends Asynctask, and in the do-in-background method another method is called so that the images are scaled down efficiently. The images are then sent to the onPostExecute (Bitmap [] bitmap) method, where they are placed in the scrollview. Where is the leak? The fading of my galaxy s3 is 64 MB, and after downloading only 9 images, the memory size increases to 42 MB, then the next click to 47 MB ... then nothing strange happens ... I think it's good .. it's good that it stops here 47 mb ... but after another 10 taps on the memory, the memory usage continues to grow to 55 mb .. and so on until the application works.
I solved this temporarily by setting large-heap = true in the manifest file. But I do not want to put off the problems!
I can not understand why the memory is too large for 9 images. And before they decrease, the average image size is about 250 kb.
Very grateful for the help !!! :-)
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; } } }