title: Warning - Memory Leak - Context classes in static fields
body: <p>I am trying to implement Architecture Components from Codelabs. But I'm getting the below error</p>
  
  <blockquote>
    <p>Do not place Android context classes in static fields (static
    reference to FarmerNetworkDataSource which has field mContext pointing
    to Context); this is a memory leak (and also breaks Instant Run)
    less...</p>
  </blockquote>
  
  <p>I used the code snippet directly from Google Codelabs. You can <a href="https://github.com/googlecodelabs/android-build-an-app-architecture-components/blob/arch-training-steps/app/src/main/java/com/example/android/sunshine/data/network/WeatherNetworkDataSource.java" rel="nofollow noreferrer">refer it here</a></p>
  
  <p>I have seen some <a href="https://stackoverflow.com/questions/37709918/warning-do-not-place-android-context-classes-in-static-fields-this-is-a-memory">duplicate questions</a>. But i can't figure out which is the best pratice to follow.</p>
  
  <p>Please help &amp; Guide me...</p>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>public class FarmerNetworkDataSource {
  
      // For Singleton instantiation
      private static final Object LOCK = new Object();
      private static FarmerNetworkDataSource sInstance;
      private final Context mContext;
  
      private final AppExecutors mExecutors;
  
      private FarmerNetworkDataSource(Context context, AppExecutors executors) {
          mContext = context;
          mExecutors = executors;
      }
  
      /**
       * Get the singleton for this class
       */
      public static FarmerNetworkDataSource getInstance(Context context, AppExecutors executors) {
          Log.d(LOG_TAG, "Getting the network data source");
          if (sInstance == null) {
              synchronized (LOCK) {
                  sInstance = new FarmerNetworkDataSource(context.getApplicationContext(), executors);
                  Log.d(LOG_TAG, "Made new network data source");
              }
          }
          return sInstance;
      }
  }
  </code></pre>
  
  <p><strong>REPOSITORY</strong></p>
  
  <pre><code>public static FarmerRepository provideRepository(Context context) {
          AppExecutors executors = AppExecutors.getInstance();
          FarmerNetworkDataSource networkDataSource =
                  FarmerNetworkDataSource.getInstance(context.getApplicationContext(), executors);
          return FarmerRepository.getInstance(networkDataSource, executors);
      }
  </code></pre>