title: "Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"
body: <p>Similar question have been <a href="https://stackoverflow.com/questions/37709918/warning-do-not-place-android-context-classes-in-static-fields-this-is-a-memory">asked here</a>, <a href="https://stackoverflow.com/questions/39868001/do-not-place-android-context-classes-in-static-fields-this-is-a-memory-leak">here</a> and <a href="https://stackoverflow.com/questions/37894622/do-not-place-android-context-classes-in-static-fields-this-is-a-memory-leak-a">here</a> but the context  is quite different from this and moreover the <a href="https://developer.android.com/training/volley/requestqueue.html#singleton" rel="noreferrer">code that gave from this error</a> is written by the makers of Android and Android Studio.</p>
  
  <p>This is the code:</p>
  
  <pre><code>public class MySingleton {
      private static MySingleton mInstance;
      private RequestQueue mRequestQueue;
      private ImageLoader mImageLoader;
      private static Context mCtx;
  
      private MySingleton(Context context) {
          mCtx = context;
          mRequestQueue = getRequestQueue();
  
          mImageLoader = new ImageLoader(mRequestQueue,
                  new ImageLoader.ImageCache() {
              private final LruCache&lt;String, Bitmap&gt;
                      cache = new LruCache&lt;String, Bitmap&gt;(20);
  
              @Override
              public Bitmap getBitmap(String url) {
                  return cache.get(url);
              }
  
              @Override
              public void putBitmap(String url, Bitmap bitmap) {
                  cache.put(url, bitmap);
              }
          });
      }
  
      public static synchronized MySingleton getInstance(Context context) {
          if (mInstance == null) {
              mInstance = new MySingleton(context);
          }
          return mInstance;
      }
  
      public RequestQueue getRequestQueue() {
          if (mRequestQueue == null) {
              // getApplicationContext() is key, it keeps you from leaking the
              // Activity or BroadcastReceiver if someone passes one in.
              mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
          }
          return mRequestQueue;
      }
  
      public &lt;T&gt; void addToRequestQueue(Request&lt;T&gt; req) {
          getRequestQueue().add(req);
      }
  
      public ImageLoader getImageLoader() {
          return mImageLoader;
      }
  }
  </code></pre>
  
  <p>The lines giving the warning are:</p>
  
  <pre><code>private static MySingleton mInstance;
  private static Context mCtx;
  </code></pre>
  
  <p>Now if I remove the <code>static</code> keyword, change <code>public static synchronized MySingleton getInstance(Context...</code> to <code>public synchronized MySingleton getInstance(Context...</code> the error disappers but another problem comes up.</p>
  
  <p>I use <code>MySingleton</code> in RecyclerView. So this line </p>
  
  <p><code>@Override
      public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
          ImageLoader imageLoader = MySingleton.getInstance(mContext).getImageLoader();</code> </p>
  
  <p>tells me </p>
  
  <blockquote>
    <p>Non-static method 'getInstance(android.content.Context)' cannot be refrenced from a static context.</p>
  </blockquote>
  
  <p>Please anybody knows how to fix this?</p>