title: How to solve the vm budget memory heap error in lazy loading bitmaps?
body: <p>I have been using Image loader class for lazy loading in list view. It works fine but I have been dealing with lots of bitmap that will be downloaded from web service. so now I get bitmap size exceeds VM budget. Increased bitmapfactory size to be 32*1024 but if it reaches 32mb again it will throw a the error. It being breaking my head for the past one week. so some one please help me out of this problem. I here by post my image loader class as well please let me know where and how should I solve this problem.</p>
  
  <pre><code>public class ImageLoader {
  
  private HashMap&lt;String, Bitmap&gt; cache=new HashMap&lt;String, Bitmap&gt;();
  Bitmap bitmap = null;
  
  private Activity activity;
  PhotosLoader photoLoaderThread=new PhotosLoader();
  public ImageLoader(Activity activity){
      this.activity = activity;
      photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
      clearCache();
  }
  public void DisplayImage(String url, Activity activity, ImageView imageView, String[] imageUrl){
      this.activity = activity;
      if(cache.containsKey(url)){
          imageView.setImageBitmap(cache.get(url));
  
      }
  else{
          imageView.setImageResource(R.drawable.icon);
          queuePhoto(url, activity, imageView);
      }    
  
  }
  
  private void queuePhoto(String url, Activity activity, ImageView imageView){
      this.activity = activity;
      photosQueue.Clean(imageView);
      PhotoToLoad p=new PhotoToLoad(url, imageView);
      synchronized(photosQueue.photosToLoad){
          photosQueue.photosToLoad.push(p);
          photosQueue.photosToLoad.notifyAll();
      }
      if(photoLoaderThread.getState()==Thread.State.NEW)
          photoLoaderThread.start();
  }
  
  public Bitmap getBitmap(String url, int sampleSize) throws Exception{
      Bitmap bm = null;
      try {
          URL request = new URL(url);
          InputStream is = (InputStream) request.getContent();
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inDither = true;
          options.inPurgeable = true;
          options.inInputShareable = true;
          options.inSampleSize = sampleSize;
          options.inTempStorage = new byte[32 * 1024];
  
          bm = BitmapFactory.decodeStream(is, null, options);
          if (bm!=null)
          bm = Bitmap.createScaledBitmap(bm, 115,100, true);
          is.close();
          is = null;
      } catch (IOException e) {
          throw new Exception();
      } catch (OutOfMemoryError e) {
          bm.recycle();
          bm = null;
          System.gc();
          throw new Exception();
      }
      return bm;
  }
  private class PhotoToLoad{
      public String url;
      public ImageView imageView;
      public PhotoToLoad(String u, ImageView i){
          url=u; 
          imageView=i;
      }
  }
  PhotosQueue photosQueue=new PhotosQueue();
  public void stopThread(){
      photoLoaderThread.interrupt();
  }
  static class  PhotosQueue{
      private Stack&lt;PhotoToLoad&gt; photosToLoad=new Stack&lt;PhotoToLoad&gt;();
      public void Clean(ImageView image){
          for(int j=0 ;j&lt;photosToLoad.size();){
              try{
              if(photosToLoad.get(j).imageView==image)
                  photosToLoad.remove(j);
              else
                  ++j;
              }catch (Exception e) {
              }
          }
      }
  }
  class PhotosLoader extends Thread {
      public void run() {
          try {
              while(true){
  
                  if(photosQueue.photosToLoad.size()==0)
                      synchronized(photosQueue.photosToLoad){
                          photosQueue.photosToLoad.wait();
                      }
                  if(photosQueue.photosToLoad.size()!=0){
                      PhotoToLoad photoToLoad;
                      synchronized(photosQueue.photosToLoad){
                          photoToLoad=photosQueue.photosToLoad.pop();
                      }
                      Bitmap bmp = null;
                          bmp = getBitmap(photoToLoad.url, 1);
                      cache.put(photoToLoad.url, bmp);
                      if(((String)photoToLoad.imageView.getTag()).equals(photoToLoad.url)){
                          BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
                          Activity a=(Activity)photoToLoad.imageView.getContext();
                          a.runOnUiThread(bd);
                      }
                  }
                  if(Thread.interrupted())
                      break;
              }
          } catch (InterruptedException e) {
              e.printStackTrace();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  
  
  class BitmapDisplayer implements Runnable{
      ImageView imageView;
      public  BitmapDisplayer(Bitmap b, ImageView i){
          bitmap = null;
          bitmap=b;
          imageView=i;
          }
      public void run(){
          if(bitmap!=null){
              imageView.setImageBitmap(bitmap);
              imageView.setScaleType(ScaleType.FIT_XY);
          }
          else{
              imageView.setImageResource(R.drawable.icon);
          }
      }
  }
  
  public void clearCache() {
      try{
      cache.clear();
      cache = new HashMap&lt;String, Bitmap&gt;();
      bitmap.recycle();   
      System.gc();
      }catch (Exception e) {
      }
  }
  </code></pre>