Android Volleyball Memory Leaks

I use the goley volley library, and now I am struggling with memory leaks in my applications for weaknesses. I have done a lot of research and have done a lot, but now I just don’t know what to do. This is a sample code:

SplashActivity.java

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);
        }
    }

}

GetTokenOnSuccessListener.java

public class GetTokenOnSuccessListener implements Response.Listener<JSONObject> {

    //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();
        }
    }
}

GetTokenOnErrorListener.java

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);
    }
}

Now I have translated the listeners of the answers to my own classes, based on what I read on the Internet, thinking that it will allow the leak, but this is not so. I added code to cancel all pending onDestroy () requests based on the request tag, but still I had memory leaks.

, , , (), , . , , 11mb.

, : - ? ?

:

compile 'com.android.volley:volley:1.0.0'
+6
3

, , . 'Com.android.volley: : 1.1.0-rc1

https://github.com/google/volley/releases/tag/1.1.0-rc1

+3

" ". Activity (mContext), . , , . , .

:

1) WeakReference<Context> , . , , , . .

2) mContext null , Activity onDestroy(). , - Context . , , , GC .

+3

. . : https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Intent mainIntent = new Intent(mContext, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(mainIntent);
-2

All Articles