title: Memory leak with <a load image in a listview> asynctask
body: <p>I acknoledged a memory leak when I saw that my small service, without any activity running, took about 15-16 MB. Normally (at the system boot) its memory usage was about 3MB, but if I opened my app memory usage increased, even after closing it!</p>
  
  <p>Looking closer this memory usage went up with number of activities I opened!
  So I payed attention to my activities: in my app I have 3 of them and everyone has a listview, whose cells' contents are loaded from the net and each element is made like this:</p>
  
  <p>|imageview |some text |</p>
  
  <p>I think problem is in the async task in the adapter, so heres the code:</p>
  
  <pre><code>public class MyAdapter extends CursorAdapter{
      private final LayoutInflater mInflater;
      private LruCache&lt;String, Bitmap&gt; cachedBitmaps;
      private SimpleDateFormat srcFormat; //I have to convert dates
      private SimpleDateFormat dstFormat;
  
  
     @Override
  public void bindView(View view, Context context, Cursor c) {
      ViewHolder holder = (ViewHolder)view.getTag();
      if(holder == null){
          holder = new ViewHolder();
          holder.cellImg = (ImageView)view.findViewById(R.id.cellImg); 
          holder.cellProgress = view.findViewById(R.id.cellProgress);
          holder.titleText = (TextView)view.findViewById(R.id.titleText);
          view.setTag(holder);
      }
              try{
          String imageLink = c.getString(c.getColumnIndex("image_link"));
          String guid = c.getString(c.getColumnIndex("guid"));
          Bitmap bitmap = cachedBitmaps.get(guid);
  
          if(bitmap == null){ //need to download
              ImageView image = holder.cellImg; 
              image.setTag(guid);
              DownloadBitmapTask task = new DownloadBitmapTask(context,image,
                      holder.cellProgress);
              task.execute(new String[]{imageLink});
                       //else I just get it from cache
  </code></pre>
  
  <p>Using the holder and the cache I make sure to efficiently load the image...I don't see any errors here but I shared this with you because I'd like to give you a complete view!
  Now here's the task, where I think there's a problem:</p>
  
  <pre><code>private class DownloadBitmapTask extends AsyncTask&lt;String, Void, Bitmap&gt;{
  
      private ImageView img;
      private View progress;
      private String tag;
              //normally you see a progress bar, when image is loaded I substitute it
      public DownloadBitmapTask(Context c,ImageView img,View progress){
          this.progress = progress;
          this.img = img;
          tag = new String(img.getTag().toString());
      }
  
      @Override
      protected void onPreExecute() {
          progress.setVisibility(View.VISIBLE);
          img.setVisibility(View.INVISIBLE);
      }
  
      private final int MAX_TRIES = 3;
  
      @Override
      protected Bitmap doInBackground(String... args) {
          Bitmap bitmap = null;
          int failedTries = 0;
          boolean done = false;
          while((!done) &amp;&amp; (failedTries&lt;MAX_TRIES)){
              try{
                  URLConnection conn = (new URL(args[0])).openConnection();
                  bitmap = BitmapFactory.decodeStream(
                          conn.getInputStream());
  
                  done = true;
              }catch(IOException ex){
                  failedTries++;
                  try {
                      Thread.sleep(100);
                  } catch (InterruptedException e) {
                  }
              }
          }
  
          return bitmap;
      }
  
  
  
      @Override
      protected void onPostExecute(Bitmap result) {
          progress.setVisibility(View.INVISIBLE);
  
          if( (result != null) &amp;&amp; (img.getTag().toString().equals(tag)) ){
              cachedBitmaps.put((String)img.getTag(), result);
              img.setImageBitmap(result);
              img.setVisibility(View.VISIBLE);
              img.getParent().recomputeViewAttributes(img);
              notifyDataSetChanged();
          }
      }
  
  }
  </code></pre>
  
  <p>I can't see any memory leak...but I'm sure there is! Please help me :(</p>
  
  <p><strong>EDIT:</strong>
  MAT tool gives me this:</p>
  
  <p>The class "android.content.res.Resources", loaded by "", occupies 5.033.704 (38,96%) bytes. The memory is accumulated in one instance of "java.lang.Object[]" loaded by "".</p>
  
  <p>They are all bitmaps, of curse :(</p>