title: Android Volley Memory Leaks
body: <p>I use google's volley library and I have been battling memory leaks in my apps for weaks now.  I have done soo much research and tried soo much already but now I just do not know what to do.  This is a sample code:</p>
  
  <p>SplashActivity.java</p>
  
  <pre><code>public class SplashActivity extends AppCompatActivity {
  
      Context mContext;
      AuthRequest mAuthRequest;
      GetTokenOnSuccessListener mGetTokenOnSuccessListener;
      GetTokenOnErrorListener mGetTokenOnErrorListener;
      private ConfigTable mConfigTable;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          initialiseViewsAndComponents();
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          getAuthToken();
      }
  
      private void initialiseViewsAndComponents() {
          mContext = SplashActivity.this;
          mAuthRequest = new AuthRequest(mContext);
          mGetTokenOnSuccessListener = new GetTokenOnSuccessListener(mContext);
          mGetTokenOnErrorListener = new GetTokenOnErrorListener(mContext);
          mConfigTable = new ConfigTable(mContext);
      }
  
      private void getAuthToken() {
          if (!mConfigTable.get("INITIALISED").equals("")) {
              mAuthRequest.guest(mGetTokenOnSuccessListener, mGetTokenOnErrorListener);
          } else {
              Intent mainIntent = new Intent(mContext, MainActivity.class);
              startActivity(mainIntent);
          }
      }
  
  }
  </code></pre>
  
  <p>GetTokenOnSuccessListener.java</p>
  
  <pre><code>public class GetTokenOnSuccessListener implements Response.Listener&lt;JSONObject&gt; {
  
      //private Activity mActivity;
      private Context mContext;
      private ConfigTable mConfigTable;
      private int mSuccess = 0;
      private String mMessage = "";
  
      public GetTokenOnSuccessListener(Context context) {
          //this.mActivity = context;
          this.mContext = context;
          this.mConfigTable = new ConfigTable(this.mContext);
      }
  
      @Override
      public void onResponse(JSONObject response) {
          try {
              mSuccess = Integer.parseInt(response.get("success").toString());
              mMessage = response.get("message").toString();
  
              if (mSuccess == 1) {
                  mConfigTable.setAuthToken(response.get("message").toString());
                  Intent mainIntent = new Intent(mContext, MainActivity.class);
                  mContext.startActivity(mainIntent);
                  ((SplashActivity) mContext).finish();
              } else {
                  Toast.makeText(mContext, "Lol access denied, could not retrieve token from server.", Toast.LENGTH_SHORT).show();
              }
  
  
          } catch (JSONException e) {
              e.printStackTrace();
              Toast.makeText(mContext, "Lol access denied, could not retrieve token from server.", Toast.LENGTH_SHORT).show();
          }
      }
  }
  </code></pre>
  
  <p>GetTokenOnErrorListener.java</p>
  
  <pre><code>public class GetTokenOnErrorListener implements Response.ErrorListener {
  
      private Context mContext;
  
      public GetTokenOnErrorListener(Context context) {
          this.mContext = context;
      }
  
      @Override
      public void onErrorResponse(VolleyError error) {
          Utils.showNetworkResponse(mContext, error);
      }
  }
  </code></pre>
  
  <p>Okay now I moved the response listeners to their own separate classes based on something I read online thinking it will resolve the leak but no it did not. I added code to cancel all pending requests <strong>onDestroy()</strong> based on the request's tag but still I had memory leaks.</p>
  
  <p>This is just my splash activity and the leaks here are small, I have a feeling it's because I call finish() but I don't get that because I call it after the request has been completed successfully.  All my other activities have similar codes but leak more memory as much as <strong>11mb</strong>.</p>
  
  <p>So my question is has anyone worked with the volley library? How do I use it and avoid memory leaks?</p>
  
  <p>Using this version:</p>
  
  <pre><code>compile 'com.android.volley:volley:1.0.0'
  </code></pre>