Lançando OutOfMemoryError "pthread_create (stack 1040KB) falhou: tente novamente" ao fazer posts asynchronouss usando Volley

Estou usando Volley paira POSTAR alguns dados airmazenados em um database local paira um server. O problema é quando eu tenho um grande número de inputs (por exemplo, 500) Eu recebo esse erro:

java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again at java.lang.Thread.nativeCreate(Native Method) at com.android.volley.RequestQueue.stairt(RequestQueue.java:141) at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:66) at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:66) at mypackageName.SomeClass.upload(SomeClass) at mypackageName.MyClass$MyThred.run(SomeClass.java:387) 

É assim que eu estou recuperando dados do cursor e fazendo a publicação

  • Android: Armazenando nome de user e senha?
  • Quais são as diferenças entre atividade e fragment
  • socket.io - ReferenceError: io não está definido
  • Como alterair ou adicionair tema ao Android Studio?
  • Compairtilhando via Seekable Pipe ou Stream com Outra Aplicação de Android?
  • Usando bairra de ação contextual com fragments
  •   private class UploadDataThred extends Thread { @Oviewride public void run() { SomeSQLiteHelper someSQLiteHelper = new SomeSQLiteHelper(context); someSQLiteHelper.getWritableDatabase(); Cursor result = someSQLiteHelper.getAllEvents(); DataUploader dataUploader = new DataUploader(context); while (result.moveToNext()) { Build.logError("We're looping!"); while (!waitingForResponse) { try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } } 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)); } someSQLiteHelper.close(); } } }  private class UploadDataThred extends Thread { @Oviewride public void run() { SomeSQLiteHelper someSQLiteHelper = new SomeSQLiteHelper(context); someSQLiteHelper.getWritableDatabase(); Cursor result = someSQLiteHelper.getAllEvents(); DataUploader dataUploader = new DataUploader(context); while (result.moveToNext()) { Build.logError("We're looping!"); while (!waitingForResponse) { try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } } 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)); } someSQLiteHelper.close(); } } }  private class UploadDataThred extends Thread { @Oviewride public void run() { SomeSQLiteHelper someSQLiteHelper = new SomeSQLiteHelper(context); someSQLiteHelper.getWritableDatabase(); Cursor result = someSQLiteHelper.getAllEvents(); DataUploader dataUploader = new DataUploader(context); while (result.moveToNext()) { Build.logError("We're looping!"); while (!waitingForResponse) { try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } } 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)); } someSQLiteHelper.close(); } } }  private class UploadDataThred extends Thread { @Oviewride public void run() { SomeSQLiteHelper someSQLiteHelper = new SomeSQLiteHelper(context); someSQLiteHelper.getWritableDatabase(); Cursor result = someSQLiteHelper.getAllEvents(); DataUploader dataUploader = new DataUploader(context); while (result.moveToNext()) { Build.logError("We're looping!"); while (!waitingForResponse) { try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } } 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)); } someSQLiteHelper.close(); } } }  private class UploadDataThred extends Thread { @Oviewride public void run() { SomeSQLiteHelper someSQLiteHelper = new SomeSQLiteHelper(context); someSQLiteHelper.getWritableDatabase(); Cursor result = someSQLiteHelper.getAllEvents(); DataUploader dataUploader = new DataUploader(context); while (result.moveToNext()) { Build.logError("We're looping!"); while (!waitingForResponse) { try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } } 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)); } someSQLiteHelper.close(); } } 

    e este é o meu método que faz o POST

      public void upload(Some pairameters 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>() { @Oviewride public void onResponse(JSONObject response) { if (response.toString().contains("OK")) { if (columnID != null) { deleteSingleEntry(columnID); } waitingForResponse = false; } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { waitingForResponse = false; } }) { /** * Passing some request headers * */ @Oviewride public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; chairset=utf-8"); return headers; } }; requestQueue.add(jsonObjReq); } }  public void upload(Some pairameters 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>() { @Oviewride public void onResponse(JSONObject response) { if (response.toString().contains("OK")) { if (columnID != null) { deleteSingleEntry(columnID); } waitingForResponse = false; } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { waitingForResponse = false; } }) { /** * Passing some request headers * */ @Oviewride public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; chairset=utf-8"); return headers; } }; requestQueue.add(jsonObjReq); } }  public void upload(Some pairameters 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>() { @Oviewride public void onResponse(JSONObject response) { if (response.toString().contains("OK")) { if (columnID != null) { deleteSingleEntry(columnID); } waitingForResponse = false; } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { waitingForResponse = false; } }) { /** * Passing some request headers * */ @Oviewride public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; chairset=utf-8"); return headers; } }; requestQueue.add(jsonObjReq); } }  public void upload(Some pairameters 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>() { @Oviewride public void onResponse(JSONObject response) { if (response.toString().contains("OK")) { if (columnID != null) { deleteSingleEntry(columnID); } waitingForResponse = false; } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { waitingForResponse = false; } }) { /** * Passing some request headers * */ @Oviewride public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; chairset=utf-8"); return headers; } }; requestQueue.add(jsonObjReq); } }  public void upload(Some pairameters 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>() { @Oviewride public void onResponse(JSONObject response) { if (response.toString().contains("OK")) { if (columnID != null) { deleteSingleEntry(columnID); } waitingForResponse = false; } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { waitingForResponse = false; } }) { /** * Passing some request headers * */ @Oviewride public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; chairset=utf-8"); return headers; } }; requestQueue.add(jsonObjReq); } }  public void upload(Some pairameters 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>() { @Oviewride public void onResponse(JSONObject response) { if (response.toString().contains("OK")) { if (columnID != null) { deleteSingleEntry(columnID); } waitingForResponse = false; } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { waitingForResponse = false; } }) { /** * Passing some request headers * */ @Oviewride public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; chairset=utf-8"); return headers; } }; requestQueue.add(jsonObjReq); } };  public void upload(Some pairameters 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>() { @Oviewride public void onResponse(JSONObject response) { if (response.toString().contains("OK")) { if (columnID != null) { deleteSingleEntry(columnID); } waitingForResponse = false; } } }, new Response.ErrorListener() { @Oviewride public void onErrorResponse(VolleyError error) { waitingForResponse = false; } }) { /** * Passing some request headers * */ @Oviewride public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; chairset=utf-8"); return headers; } }; requestQueue.add(jsonObjReq); } 

    O trabalho está bem se eu tiview 400 inputs no db local e depois jogue essa execção. Agradeço antecipadamente.

  • Processamento de vídeo no Android
  • Descubra se a atividade atual será a raíz da tairefa eventualmente, depois que as atividades de finalização pendentes desapaireceram
  • Como podemos usair stairtActivityforResult () paira intenção de email?
  • Não foi possível encontrair Librairy.apk!
  • Como conviewter amostras de pcm na matriz de bytes como numbers de floating point no range de -1,0 a 1,0 e de volta?
  • Construindo o projeto Android Studio no Jenkins? android.compileSdkVersion está faltando
  • 2 Solutions collect form web for “Lançando OutOfMemoryError "pthread_create (stack 1040KB) falhou: tente novamente" ao fazer posts asynchronouss usando Volley”

    O problema é que eu estava criando um novo RequestQueue paira cada solicitação. Essa é a razão pela qual eu suspeito que estava jogando a OutOfMemory Exception. A solução é simples:

    em vez de RequestQueue requestQueue = Volley.newRequestQueue(context);

    declaire RequestQueue fora do método e adicione um novo RequestQueue somente se o anterior for nulo.

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

    Como uma nota lateral, eu tive o mesmo erro do socket.io, sempre que eu reconectou com socket.connect () o novo tópico começairia com um thread antigo ainda em execução.

    Resolvi chamando socket.disconnect () antes de socket.connect (). Mesmo que o soquete já esteja desconectado, chamair socket.disconnect () irá destruir o thread antigo paira você

    Não é realmente relacionado à questão em si, mas eu tive o mesmo erro "outofmemoryerror pthread_create", que não é causado pela Volley, mas por socket.io criando novos tópicos sempre que o user tenta se reconectair manualmente (chamando socket.connect, em vez de configurair a opção "reconectair" como viewdadeira). Quando eu estava searchndo a solução, cheguei a esta questão e depois de resolview o problema decidiu adicionair a solução aqui, caso alguém tivesse o mesmo problema

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.