I\'m using Volley to POST some data stored in a local database to a server. The problem is when I have a big number of entries (for example 500) I get this error:
The problem is I was creating a new RequestQueue for every request. That's the reason I suspect it was throwing the OutOfMemory Exception. The solution is simple:
instead of RequestQueue requestQueue = Volley.newRequestQueue(context);
declare the RequestQueue outside the method and add a new RequestQueue only if the previous one is null.
private RequestQueue requestQueue;
public void uploadData(String s) {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(context);
Build.logError("Setting a new request queue");
} more request stuff....
}
I maybe a bit late here.
The issue arises when new RequestQueue
is being added unnecessarily and it just consumes a lot of memory.
The trick is to check whether the requestqueue is null and if so create a new request or if it already exist just add to the existing request.
In your code, do something like this:
private RequestQueue requestQueue;// Declare
//In your Volley Call Method
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsArrRequest);
} else {
requestQueue.add(jsArrRequest);
}
As a side note, I had the same error from socket.io, whenever I reconnected with socket.connect() new thread would start with old thread still running.
Solved it by calling socket.disconnect() before socket.connect(). Even though socket is already disconnected, calling socket.disconnect() will destroy old thread for you
It is not actually related to the question itself, but I had the same "outofmemoryerror pthread_create" error, which isn't caused by Volley but by socket.io creating new threads every time user tries to reconnect manually (by calling socket.connect, instead of setting "reconnect" option to true). When I was googling for the solution I came to this question and after fixing the problem decided to add the solution here, just in case someone else had the same problem