title: ListView with custom adapter, two types of items and one type with a image gallery. Heap growing up until OutOfMemoryError
body: <p><br>
  I am developing a very complex screen. It has a listview with two types of items: first item, a googlemap and the rest, details of markers in map.<br>
  These details are cells with text data and a gallery (<a href="https://github.com/davidschreiber/FancyCoverFlow" rel="nofollow">fancycoverflow</a>) and its images are downloaded using Picasso library.<br>
  When the user moves to another city in the map, the list is refreshing using adapter.notifyDataSetChanged();<br>
  It works perfectly: all text data and images are displayed correctly, but memory ram is growing up each time the user moves to another city or scrolls the list. After some time, it throws an OutOfMemoryError.<br></p>
  
  <p>I cannot show the full code but the getView of the main adapter is something like this:</p>
  
  <pre><em>MainAdapter</em><br>
  getView() {<br>
  &nbsp;&nbsp;switch(type) {<br>
  &nbsp;&nbsp;&nbsp;&nbsp;case 0: // map<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... viewholder pattern...<br>
  &nbsp;&nbsp;&nbsp;&nbsp;break;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;case 1: // detail with fancycoverflow<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... viewholder pattern...<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;holder.textView.setText("...");<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;holder.fancy.setAdapter(new FancyCoverFlowCustomAdapter(ctx, images);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>
  &nbsp;&nbsp;&nbsp;&nbsp;break;<br>
  &nbsp;&nbsp;}<br>
  }<br></pre>
  
  <p>The whole text is much long. It has some <em>if</em> conditions to display some textviews or not.
  I think the problem is in the setAdapter line. I have tried checking if adapter exists and not creating a new one but images are displaying in the wrong cell.</p>
  
  <p>The FancyCoverFlowCustomAdapter getCoverFlowItem method is:</p>
  
  <pre><em>FancyCoverFlowCustomAdapter</em><br>
  getCoverFlowItem() {<br>
  &nbsp;&nbsp;ImageView imageView = null;<br>
  &nbsp;&nbsp;if (reuseableView != null) {<br>
  &nbsp;&nbsp;&nbsp;&nbsp;imageView = (ImageView) reuseableView;<br>
  &nbsp;&nbsp;} else {<br>
  &nbsp;&nbsp;&nbsp;&nbsp;imageView = new ImageView(viewGroup.getContext());<br>
  &nbsp;&nbsp;&nbsp;&nbsp;imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;imageView.setLayoutParams(new FancyCoverFlow.LayoutParams(FancyCoverFlow.LayoutParams.MATCH_PARENT,
                      FancyCoverFlow.LayoutParams.WRAP_CONTENT));<br>
  &nbsp;&nbsp;}<br>
  
  &nbsp;&nbsp;if (imageView.getDrawable() == null) {<br>
  &nbsp;&nbsp;((HomeActivity) context).picasso.load(someurl).config(Bitmap.Config.RGB_565).placeholder(R.drawable.placeholder_loader).resize(((HomeActivity) context).screenWidth, ((HomeActivity) context).cellHeight).centerCrop()
                      .error(R.drawable.placeholder).into(imageView);<br>
  &nbsp;&nbsp;}<br>
  &nbsp;&nbsp;return imageView;<br>
  }<br></pre>
  
  <p>Picasso object is created in HomeActivity:</p>
  
  <pre>picasso = new Picasso.Builder(this).executor(Executors.newSingleThreadExecutor()).build();</pre>
  
  <p>The RGB_565 config is used to get better image performance.</p>
  
  <p>I am new on StackOverFlow forum. I will try to update this post to show the little code I have posted as clear as possible, or paste all code I can.</p>
  
  <p>I appreciate so much for your help.</p>
  
  <h3>ADITIONAL INFORMATION: a sample workflow:<br></h3>
  
  <p>1 - The app is started: first city is selected, points of interest are downloaded using Retrofit and markers in map and list is generated. Images per cell are displayed in fancycoverflow gallery.<br>
  Heap: 32Mb / 39Mb<br>
  2 - The user moves to another city, new POIs are downloaded, adapter is notified about the changes so list items changes and new images in galleries are shown.<br>
  Heap: 36 / 42Mb<br>
  3 - The user moves to another city, same process.<br>
  Heap: 41 / 44Mb<br>
  And so on.<br></p>
  
  <p>I think the problem may be bitmaps, that are not correctly recycled. I have searched about how to avoid it with Picasso, but the only thing I have found is using 1 thread and skipping memory cache. Both of them (and separately) didn't work for me, heap size is growing up continuously.<br></p>
  
  <p>I have read how to load bitmaps efficiently but I think this is not the problem. Bitmaps are loaded by Picasso using resize() method. In addition, Memory Analyzer shows a 70 bitmap allocation after visiting three cities. <br></p>
  
  <p>Moreover, MAT also shows lots of String allocatiosn (about 12.000). Is it possible that Retrofit, Gson or JSONObject could leak some data? I use all of them to parse server response.<br><br>
  Thank you very much.</p>