title: Image adapter leaking memory
body: <p>I have a simple <em>ListActivity</em> that shows images and I inizialize my <em>OkHttpClient</em> for <em>Picasso Builder</em> in the constructor of the ImageAdapter class:</p>
  
  <pre><code>picassoClient = new OkHttpClient();
  picassoClient.interceptors().add(new Interceptor() {
      @Override
      public Response intercept(Chain chain) throws IOException {
          Request newRequest = chain
              .request()
              .newBuilder()
              .addHeader("Cookie","xyz")
              .build();
  
          return chain.proceed(newRequest);
      }
  });
  
  new Picasso.Builder(context).downloader(new OkHttpDownloader(picassoClient)).build();
  </code></pre>
  
  <p>then in <code>getView()</code> I use Picasso to load images in ImageView:</p>
  
  <pre><code>Picasso.with(context).load(xyzUrl).fit().centerCrop().into(vImage);
  </code></pre>
  
  <p>It works well, but on device's rotation i see that <strong>heap size</strong> sometimes slowly grows, sometimes quickly and sometimes remains stable. Only rarely it drops. Am i leaking memory or is there something wrong in code?</p>
  
  <p><strong>EDIT:</strong>
  I inserted this code after Picasso's call in the <code>getView()</code></p>
  
  <pre><code>if (BuildConfig.DEBUG) {
      Log.i("HEAP SIZE",
      String.valueOf((Runtime.getRuntime().totalMemory() / 1024)
      - (Runtime.getRuntime().freeMemory() / 1024)));
  }
  </code></pre>
  
  <p>and I found that the heap size's growth happens in the <code>getView()</code> after loading bitmap into ImageView.
  What is wrong?</p>
  
  <p><strong>EDIT 2</strong>: 
  tried to set static ImageAdapter, nothing changes</p>
  
  <p><strong>EDIT 3:</strong>
  tried with RecyclerView instead of ListView, same behavior: heap size grows continuously while scrolling image list stepping by 30-40 bytes at every <code>onBindViewHolder()</code>. After device's rotation heap size grows sometimes stepping by even 2-3 Mbytes. Rarely it drops.</p>
  
  <p>Why heap size slowly but continuously grows and why am I leaking some cache or some cached bitmaps after device's rotation?</p>
  
  <p><strong>UPDATE:</strong>
  tried adapter without the code in the constructor (that is without <code>new OkHttpClient</code> and <code>new Picasso.Builder</code>), it works and <strong><em>the heap size now drops well remaining stable</em></strong>. Then, what is the correct way to initialize the client with cookies headers management?</p>
  
  <p><strong>UPSHOT:</strong>
  finally I created my PicassoInstance class, which creates a unique static Picasso singleton and set it as the Picasso Library's singleton. Then I set it in my adapter constructor</p>
  
  <p><code>PicassoInstance.setPicassoSingleton(context);</code></p>
  
  <p>It works well, and it is a correct way I hope.</p>
  
  <pre><code>public class PicassoInstance {
  private static Picasso myPicassoInstance = null;
  
  public static void setPicassoSingleton(Context context) {
      if (myPicassoInstance == null) {
          myPicassoInstance = createMyPicassoInstance(context);
          Picasso.setSingletonInstance(myPicassoInstance);
          if (BuildConfig.DEBUG) {
              Log.i("PICASSO INSTANCE", "CREATED");
          }
      }
  }
  
  private static Picasso createMyPicassoInstance(Context context) {
      OkHttpClient myOkHttpClient = new OkHttpClient();
      myOkHttpClient.interceptors().add(new Interceptor() {
          @Override
          public Response intercept(Chain chain) throws IOException {
              Request newRequest = chain.request().newBuilder()
                      .addHeader("Cookie", "xyz").build();
              if (BuildConfig.DEBUG) {
                  Log.i("ON INTERCEPT", "COOKIE ADDED");
              }
              return chain.proceed(newRequest);
          }
      });
  
      return new Picasso.Builder(context).downloader(
              new OkHttpDownloader(myOkHttpClient)).build();
  }
  </code></pre>
  
  <p>}</p>