Throwing OutOfMemoryError "pthread_create (1040KB stack) failed: Try again" when doing asynchronous posts using Volley

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....
}

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