Volley framewok request keeps objects in memory

佐手、 提交于 2019-11-28 12:25:56

问题


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:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/25868608/volley-framewok-request-keeps-objects-in-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!