2

I'm using volley to get a JsonObject from a URL. My problem is that often I get an OutOfMemory Exception. I used to MAT to analyze the memory leaks and I see that the whole object on response after downloaded and used it is still on memory. What I'm doing wrong?

Volley Code

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    item = parser.parse(response);
                    updateLayout();

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            bar.setVisibility(View.GONE);
            if(!isNetworkAvailable()){
                Toast.makeText(act, "No Internet Connection.", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(act, "The Server Is Down. Try Again.", Toast.LENGTH_SHORT).show();
            }
        }
    });

    // Adding request to request queue
    VolleySingleton.getInstance().addToRequestQueue(jsonObjReq);

MAT screenshot

enter image description here

billiout
  • 695
  • 1
  • 8
  • 22
  • do you mean that those 3 items are shown is in memory and volley has not removed them? – mmlooloo Aug 14 '14 at 08:44
  • 1
    Yes. After onResponse is done the object from json(where the 3 items are in) is not deleted and is still on the memory for the rest of the activity. – billiout Aug 14 '14 at 08:50

2 Answers2

2

Ok i think your problem is here:

Network dispatcher is a thread that his duty is to send the request to the server and getting results, he manages the http connection and he is your outside interface, so each objects in this thread is those who their request are in the flight and have been sent to the server and are waiting to get back the result , these are other requests no the one that you get the result in on response. Network response is the one that manages parsing the response and delivers it to you after response delivered to network dispatcher. i suggest you to look at the source code to better understand this beautiful framework. i think your problem is coused by parser or your long json so you can eliminate it by providing bigger cache for volley or another joson parser technique.

Update:

i mean that those hashmaps are not memory leak, those are your other requests to the server, you have not specify any memory leak in your image. what you have shown on the picture is your other requests that you sent to volley. when you send your request to this library, this library creates multiple request connections to the server to make your network operation be speedy. those multiple requests take time to go from your device to the server and come back with response. those request and connection are all managed by network dispatcher. it makes multiple connections and sends requests to the server. so what you see on the image is those requests that have not gotten results yet from server. so those 3 items must be into the memory because those are requests that you want the results for them.

after you exit from response block {} imean last brace your response automatically removed from memory, so do not worry about your response. i think your problem dose not relate to volley buges or any thing else about this library. i think you must check other aspect of your apps like the method of parsing json or ...

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • I'm sorry I'm new on this and I didn't understand exactly what did you mean... can you explain it? I'll have to try other lib from volley? I want to release memory after onResponse so the HashMap with the objects from the Json made it from volley no longer active. – billiout Aug 14 '14 at 09:22
  • 1
    i mean that those hashmaps are not memory leak, those are your other requests to the server, you have not specify any memory leak in your image. – mmlooloo Aug 14 '14 at 13:17
  • you can post your exception OOM logcat in to Stackoverflow, there are many professional developers that may help you easily. – mmlooloo Aug 14 '14 at 18:23
  • 1
    Mmh.. I understand Volley network implementation. But what if our request requires a 2M response for example? I've an activity that fires a request on its onCreate. I've tried opening and closing it for a bunch of time. After a while, volley causes the OOM. – edoardotognoni Sep 16 '14 at 11:52
  • @edoardotognoni please ask it on stackoverflow, because i do not know exactly what is your problem? and also post logcat and your code, i or others may help you :-) – mmlooloo Sep 16 '14 at 11:59
  • Don't worry. I solved it. I simply switched to Retrofit :D MY problem was exactly billiout's problem. Volley keeps in memory its request and after a while it causes a OOM. I switched to retrofit, same code, same requests and it's working – edoardotognoni Sep 16 '14 at 12:06
  • @edoardotognoni you first said it keeps 2MB RESPONSE and now you said it keeps REQUESTS, sorry but i think your code might have some issues, if you have it just ask, i will be glad to learn from it. – mmlooloo Sep 16 '14 at 12:11
  • Well, my MAT shows that I have 'n' NetworkDispatcher in memory. All of them with the same request. Obviously, with the request, there is also the responseListener attacched, so more bytes in memory. Check this out: http://imgur.com/NNP5IFh and this: http://imgur.com/EOGkzvE – edoardotognoni Sep 16 '14 at 12:12
  • @edoardotognoni cool but still i do not know whats going there, ask it please, please and please !! believe it, its free, just ask with code and logcat and your image. – mmlooloo Sep 16 '14 at 12:19
  • @edoardotognoni and also i still think it is not volley problem, it is your logic about handling volley or other part of your code! – mmlooloo Sep 16 '14 at 12:21
  • http://stackoverflow.com/questions/25868608/volley-framewok-request-keeps-objects-in-memory – edoardotognoni Sep 16 '14 at 12:21
1

After finising my app leak search, I finally understand why volley keeps request in memory . In my case, I use volley singleton mode. Volley has 4 threads do network dispatch, and each thread keeps a request.

when you do more than 4 times request, there will always be 4 requests keep in memory. without other leak problem ,request would not raise . Usually, we use new Anonymous Inner Class request , which will keep a strong reference to outer class . so we will see Activity not GC after onDestory().

As a result, Volley not have big problem, if you do not want request Keep strong reference to your class ,so use weakReference. Your OOM problem must be something else caused.

pat8719
  • 1,700
  • 1
  • 26
  • 47
Fantasy_RQG
  • 143
  • 1
  • 13
  • Volley has fix the leak problem ,please check here [the fix volley issue](https://github.com/mcxiaoke/android-volley/pull/64) – Fantasy_RQG Jul 14 '15 at 05:47