Search code examples
androidandroid-volleyandroid-networking

Volley framewok request keeps objects in memory


I'm making a volley request in this way:

   public void makeRequest(BaseRequest request, Response.Listener<JSONObject> responseListener,
                            Response.ErrorListener errorListener) {
        if (Constants.DEBUG) Log.i(TAG, "Sending: " + request.getUrlRequest());
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(METHOD, request.getUrlRequest(), null, responseListener, errorListener);
        // disable cache
        jsObjRequest.setShouldCache(false);
        jsObjRequest.setTag(mTag);
        mQueue.add(jsObjRequest);
    }

mTag is a Class type. I have an activity where on its onCreate method I call the volley request with this:

mVolleyManager.makeRequest(getRequest(), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            refreshLayout.setRefreshing(false);
            onEndLoading(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            refreshLayout.setRefreshing(false);
            onErrorLoading(error);
        }
    });

If I start open and closing the activity for a bunch of time, my memory keeps growing till it reaches an OOM error. I tried to have a look with MAT and here's the result:

enter image description here

enter image description here

It seems that Volley keeps in memory all of its requests, even if the onResponse method is correctly called. I already solved the problem by switching to Retrofit. Same code, same requests and it's working but I want to understand what could be the cause of my problem.


Solution

  • NetworkDispatcher has only 4 threads by default so i think your problem is caused by creating multiple requestqueue and old threads dose not killed, use singleton queue, for refering to that look at:

    http://developer.android.com/training/volley/requestqueue.html

    each request queue has only 4 threads but in your image i can see more than 4 so obviously your problem caused by not using singleton design pattern.