title: android - out of memory
body: <p>In my application i am downloading images from the web. i got the out memory exception. somebody suggests that clear heap using the following code. But still i am facing the out of memory exception.can anybody help me.
  The following method is called when the activity is started. Here I refer to this <a href="https://stackoverflow.com/questions/3238388/android-out-of-memory-exception-in-gallery">link</a></p>
  
  <pre><code>  public void onStart()
  {       
       super.onStart();  
       //Handling the out of memory exception
       logHeap(this.getClass());
  }    
  
  public static void logHeap(Class clazz) {
      Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576));
      Double available = new Double(Debug.getNativeHeapSize()/1048576.0);
      Double free = new Double(Debug.getNativeHeapFreeSize()/1048576.0);
      DecimalFormat df = new DecimalFormat();
      df.setMaximumFractionDigits(2);
      df.setMinimumFractionDigits(2);
  
      System.out.println("debug. =================================");
      System.out.println("debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free) in [" + clazz.getName().replaceAll("com.myapp.android.","") + "]");
      System.out.println("debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)");
      System.gc();
      System.gc();       
  }
  </code></pre>
  
  <p>My Code:</p>
  
  <p>@Override</p>
  
  <pre><code> public View getView(final int position, View convertView, ViewGroup parent) 
  
   {          
  
           ViewHolder holder;                                     
           convertView = null;            
  
          if (convertView == null) 
          {
  
              convertView = mInflater.inflate(R.layout.giftcategorieslist, null);                            
              holder = new ViewHolder();                                       
              holder.imgitem = (ImageView)convertView.findViewById(R.id.imgitem);                            
              convertView.setTag(holder);
  
           } 
  
  
           else 
          {
              holder = (ViewHolder) convertView.getTag(); 
          }
  
           final CategoryData Item = arItems.get(position);           
          strItemrow = Item.toString();
  
          try 
          {
  
              if(Item.dwgImage == null)
              {
                  if(Item.bImageDownLoaded == 0)
                  {                       
                       holder.progress.setVisibility(View.VISIBLE);                                           
                       DownLoadImageInAThreadHandler(Item, holder); 
                  }
                  else
                  {
                      int idNoImage = R.drawable.giftsuggestionsnoimage;
                      Drawable dwgImgLoading = GiftCategories.this.getResources().getDrawable(idNoImage);
                      holder.imgitem.setImageDrawable(dwgImgLoading);
                      holder.imgitem.setVisibility(View.VISIBLE);
                      holder.progress.setVisibility(View.GONE);   
                  }
              }  
              else
              {
                  holder.imgitem.setImageDrawable(Item.dwgImage);
                  holder.imgitem.setVisibility(View.VISIBLE); 
                   holder.progress.setVisibility(View.GONE);
              }
      }
      catch(Exception e) 
      {  
          System.out.println("Exception in Downloading image : " + e.getMessage());
  
      }                               
          return convertView;
      }
  
       public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
          {
              nImageDownLoads++;
  
              System.out.println("The images being downloaded :" + nImageDownLoads);
  
  
  
               final Handler handler = new Handler() 
               {             
                   @Override  public void handleMessage(Message message) 
                   {                 
                      holder.imgitem.setImageDrawable((Drawable) message.obj);   
                      holder.imgitem.setVisibility(View.VISIBLE);
                      holder.progress.setVisibility(View.GONE);
                   }
               };
  
              //Thread for getting the attributes values
              Thread t = new Thread() 
              {
                  public void run()
                  {                       
                      try
                      {
                          Item.bImageDownLoaded = 2;
                          System.out.println("Downloading image : " + Item.ImageUrl);
                          InputStream is = fetch(Item.ImageUrl);
                          Drawable drawable = Drawable.createFromStream(is, "src");                                                   
                          nImageDownLoads--;
                          System.out.println("Downloaded image :" + Item.ImageUrl);
                          System.out.println("Remaining images for downloading: " + nImageDownLoads);
                           if(drawable != null)
                           {
                              Item.dwgImage = drawable;   
  
                              Item.bImageDownLoaded = 1;
  
                              //Send the message to the handler
                              Message message = handler.obtainMessage(1, drawable);                 
                              handler.sendMessage(message); 
  
                           }
                           else
                           {
                              int idNoImage = R.drawable.giftsuggestionsnoimage;
                              Drawable dwgNoImg = GiftCategories.this.getResources().getDrawable(idNoImage);
  
                              //Send the message to the handler
                              Message message = handler.obtainMessage(1, dwgNoImg);                 
                              handler.sendMessage(message); 
                           }                       
  
                      }
                      catch(Exception exp)
                      {
                          System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
                      }
                  }                                               
              };
              t.start();  
  
  
          }
  
      private InputStream fetch(String urlString) throws MalformedURLException, IOException 
      {         
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpGet request = new HttpGet(urlString);
          HttpResponse response = httpClient.execute(request);
          return response.getEntity().getContent();
      } 
  
      class ViewHolder 
      {           
         ImageView imgitem;                      
      }
  
   }
  </code></pre>
  
  <p>}</p>
  
  <p>thanks</p>