Adrian Antal Adrian Antal -4 years ago 430
Android Question

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

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:

java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
at java.lang.Thread.nativeCreate(Native Method)
at mypackageName.SomeClass.upload(SomeClass)
at mypackageName.MyClass$

This is how I'm retrieving data from the cursor and doing the post

private class UploadDataThred extends Thread {

public void run() {
SomeSQLiteHelper someSQLiteHelper = new SomeSQLiteHelper(context);
Cursor result = someSQLiteHelper.getAllEvents();
DataUploader dataUploader = new DataUploader(context);

while (result.moveToNext()) {
Build.logError("We're looping!");
while (!waitingForResponse) {
try {
} catch (InterruptedException e) {

dataUploader.upload(result.getString(0), false, result.getString(1), result.getString(2), result.getString(3), result.getString(4), result.getString(5), result.getString(6), result.getString(7), result.getString(8));


and this is my method that does the POST

public void upload(Some parameters go here) {

waitingForResponse = true;
VolleyLog.DEBUG = false;

final JSONObject o = new JSONObject();
o.put(Some data)

RequestQueue requestQueue = Volley.newRequestQueue(context);

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, HOST, o, new Response.Listener<JSONObject>() {

public void onResponse(JSONObject response) {

if (response.toString().contains("OK")) {

if (columnID != null) {



waitingForResponse = false;


}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {

waitingForResponse = false;

}) {

* Passing some request headers
* */
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;




It work's fine if I've got 400 entries in the local db and after that it throws that execption.
Thank you in advance.

Answer Source

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....
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download