title: Out Of Memory Exception occuring in android even after recycling Bitmap
body: <p>i have read many threads on this topic and tried to use them in my code but i am still not able to solve it.</p>
  
  <p><strong>What i am trying to do:</strong>
  Like facebook or any other app which downloads feed from server which includes image, I am also doing same and trying to display feeds and images on the image view.</p>
  
  <p>What is happening:
  I am able to download the feeds (in JSON) which includes URL of images and i am able to show them on ImageView.
  In Emulator my MAX heap size is 64MB. I am consuming around 30MB in first 10 feeds( not sure why but this is what i get from Memory tab in Android monitor of Android Studio and Even in Android Device monitor).
  I have a refresh button in my app which reloads the same feeds after removing all feeds which were earlier populated. I expected that i will be consuming the same memory or some what more. But contrary to that my memory usage got increased upto 42MB. Hence after tapping on refresh for 3 to 4 times, it is causing OutOFMemory Execption. Even if i load next 10 feed or 50 feeds at a time i am getting OutOfMemory Exception.</p>
  
  <p>I know that facebook instagram and many more such apps does the same thing but not sure how they implemented the code to cover this situation.</p>
  
  <p>Below is my code for populating feed</p>
  
  <pre><code>private void loadFeed(List&lt;Feed&gt; feedList)
  {
      Log.v(Constant.TAG,"Loading Feed in social feed page");
      for(final Feed feed:feedList) {
          LinearLayout feedBox = new LinearLayout(this);
          feedBox.setOrientation(LinearLayout.VERTICAL);
          FrameLayout profileDetailContainer= new FrameLayout(this);
          LinearLayout profileDetailContainerParent=new LinearLayout(this);
          LinearLayout profileDetailContainerChild=new LinearLayout(this);
          profileDetailContainerChild.setOrientation(LinearLayout.VERTICAL);
          ImageView imgProfile= new ImageView(this);
          TextView txtDate= new TextView(this);
          TextView txtName= new TextView(this);
          ImageView imgProduct= new ImageView(this);
          txtName.setText(feed.getUserFullName());
          TextView txtDesciption= new TextView(this);
          txtName.setTypeface(null, Typeface.BOLD);
          if(feed.getDescription().length()&gt;Constant.NUMBER_OF_DESCRIPTION_CHAR_SHOW_ON_RESULT)
          {
              txtDesciption.setText(feed.getDescription().substring(0,Constant.NUMBER_OF_DESCRIPTION_CHAR_SHOW_ON_RESULT)+"...");
          }
          else
              txtDesciption.setText(feed.getDescription());
  
          if(!IOUtil.fileExists(this,feed.getProductImageName())) {
              WebRequest request = new WebRequest();
              request.setUrl(Constant.ROOT_APPLICATION_URL_WITH_SEPARATOR + feed.getImgPath());
              request.setParam(feed.getId() + "");
              new ImageDownloadTask(this, true, feed.getProductImageName(), this).execute(request);
              Log.v(Constant.TAG,"URL:"+Constant.ROOT_APPLICATION_URL_WITH_SEPARATOR+feed.getImgPath());
              Picasso.with(getApplicationContext()).load(R.drawable.logo).into(imgProduct);
              feedBox.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      ScreenUtility.alertException(v.getContext(),"Please wait untill product image loads");
                  }
              });
              PixyfiSession.save(feed.getId() + "", imgProduct);
              PixyfiSession.save(feed.getId() + "_feed", feed);
  
          }
          else
          {
              ImageUtil.recycleIfPossible(imgProduct);
              try {
                  imgProduct.setImageBitmap(ImageUtil.getLocalImage(feed.getProductImageName(),this));
                  FeedboxOnClickListener feedboxOnClickListener = new FeedboxOnClickListener(feed);
                  feedBox.setOnClickListener(feedboxOnClickListener);
              } catch (FileNotFoundException e) {
                  Log.v(Constant.TAG,e.getMessage(),e);
              }
  
          }
  
          if(FacebookUtil.localProfilePicExists(feed.getUserName(),this))
          {
              Bitmap profileImage= FacebookUtil.getLocalProfilePicture(feed.getUserName(),this);
              imgProfile.setImageBitmap(profileImage);
          }
          else {
              FacebookUtil.loadProfilePicture(feed.getUserName(),this,this);
              PixyfiSession.save(feed.getUserName(),imgProfile);
              imgProfile.setImageResource(R.drawable.profile);
          }
          try {
  
              if(feed.getDate()==null) {
                  txtDate.setText(new SimpleDateFormat("dd MMM yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse(feed.getFeedDate())));
              }
              else {
                  txtDate.setText(new SimpleDateFormat("dd MMM yyyy").format(feed.getDate()));
              }
          } catch (ParseException e) {
              Log.e(Constant.TAG,e.getMessage(),e);
          }
          LinearLayout.LayoutParams layoutParam= new LinearLayout.LayoutParams(140,140);
          layoutParam.setMargins(5,5,0,0);
          imgProfile.setLayoutParams(layoutParam);
  
          layoutParam= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.TOP|Gravity.LEFT);
          layoutParam.setMargins(20,5,0,0);
          txtName.setLayoutParams(layoutParam);
          layoutParam= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.LEFT);
          layoutParam.setMargins(20,5,0,0);
          txtDate.setLayoutParams(layoutParam);
  
          profileDetailContainerParent.addView(imgProfile);
          profileDetailContainerChild.addView(txtName);
          profileDetailContainerChild.addView(txtDate);
          profileDetailContainerParent.addView(profileDetailContainerChild);
          feedBox.addView(profileDetailContainerParent);
          LinearLayout.LayoutParams feedLayoutParam=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL);
          feedLayoutParam.setMargins(5,5,5,5);
          imgProduct.setLayoutParams(feedLayoutParam);
          txtDesciption.setLayoutParams(feedLayoutParam);
          feedBox.addView(txtDesciption);
          feedBox.addView(imgProduct);
          feedLayoutParam=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL);
          feedLayoutParam.setMargins(0,5,0,5);
          feedBox.setLayoutParams(feedLayoutParam);
  
          feedBox.setBackgroundColor(Color.parseColor("#cccccc"));
          PixyfiSession.save(feed.getId()+"_feedbox",feedBox);
          imgProfile.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  PixyfiSession.save(Constant.SELECTED_USER_ID,feed.getUserName());
                  ScreenUtility.loadScreen(v.getContext(),UsersProfile.class,false);
              }
          });
          this.feedContainer.addView(feedBox);
  
      }
      if(feedList.size()==Constant.FEED_SIZE_IN_ONE_REQUEST)
      {
          Button moreFeed= new Button(this);
          moreFeed.setText("Load MOre Feed");
          moreFeed.setOnClickListener(new MoreFeedButtonListener(this));
          this.feedContainer.addView(moreFeed);
      }
      this.progressBar.setVisibility(View.INVISIBLE);
  }
  </code></pre>
  
  <p>For reloading/refreshing the feed</p>
  
  <pre><code>this.currentPage=1;
  this.recycleImages();
  this.feedContainer.removeAllViews();
  PixyfiSession.save(Constant.CURRENT_FEED_PAGE,this.currentPage);
  this.progressBar.setVisibility(View.VISIBLE);
  loadFeed();
  </code></pre>
  
  <p>recycleImages Method:</p>
  
  <pre><code>private void recycleImages()
  {
      for(int i=0;i&lt;this.feedContainer.getChildCount();i++)
      {
          if(this.feedContainer.getChildAt(i) instanceof  ImageView)
          {
              ImageView view=(ImageView)this.feedContainer.getChildAt(i);
              ImageUtil.recycleIfPossible(view);
          }
      }
  }
  </code></pre>
  
  <p>If you need further details on the code then please let me know.
  Also is it possible to see memory usage of other apps like facebook in android device monitor?
  <strong>UPDATE</strong>
  ImageUtil.getLocalImage Method</p>
  
  <pre><code>public static Bitmap getLocalImage(String imageName, Context context) throws FileNotFoundException
  {
      Bitmap bitmap=null;
      InputStream is=null;
      if(IOUtil.fileExists(context,imageName)) {
          is = context.openFileInput(imageName);
          bitmap= BitmapFactory.decodeStream(is);
      }
      else {
          throw new FileNotFoundException("Image file doesn't exists");
      }
      return bitmap;
  }
  </code></pre>