Note that there are some explanatory texts on larger screens.

plurals
  1. POmemory-leak and out-of-memory
    primarykey
    data
    text
    <p>Its really frustrating - I have som kind of memory leak in my app, despite I recycle bitmaps and use the garbagecollector. In my app I have a listview and when one klicks on a subitem several images loads into an alerdialogbuilder and in a scrollview. I have no more than 9 images in the scrollview and after a while I get out-of-memory-exception. What could I do more than recycle bitmaps? Should I create a weakreference of the imageviews for instance, so the garbagecollector can take care of them? </p> <p>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 efficently. The images are then sent to the method onPostExecute(Bitmap[] bitmap) where they are put into the scrollview. Where is the leak? The heapsize of my galaxy s3 is 64 mb and after loading just 9 images the memoryusage is soaring to maybe 42 mb, then next klick to 47 mb ... then nothing happends strangely ... I think, okay .. good it stops here on 47 mb ... but after 10 more clicks memory usage continue to climb to maybe 55 mb .. and so on til the app crashes.</p> <p>I have solved it temporarily by setting large-heap = true in the manifestfile. But i dont like to postpone problems!</p> <p>I cannot either understand why the memoryusage is so great for 9 images. And before they are scaled down the average size of an image is about 250kb. </p> <p>Very greatful for help!!! :-) </p> <pre><code> class BitmapWorkerTask extends AsyncTask &lt;Integer, Void, Bitmap[]&gt; { 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 &lt; 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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload