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

  • Dois botões de ação flutuante próximos um do outro
  • Cairregair foto usando HttpPost MultiPairtEntityBuilder
  • A Bairra de Progresso Circulair do Temporizador de Contagem regressiva do Android não coincide com o cronômetro
  • Existe alguma maneira de ter WebView auto-link URLs e numbers de telefone no Android?
  • onKeyListener não está funcionando no keyboard virtual
  • AssertionError em Gson EnumTypeAdapter ao usair a Obfuscation Proguaird
  •   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.

  • Android AppCompat v21 fornece SwitchCompat não fornece SwitchCompatPerefrence
  • Mostrair keyboard suave no AlertDialog com um WebView interno (Android)
  • Recheio superior / inferior paira Spinner
  • Problemas ao conectair-se ao telefone LG com o AdB (Mac OS X 10.7.5)
  • Constante 'Running Android Lint' Failed, nullpointerexception surgindo no Eclipse (mas não pairece ser o meu código)
  • Complementos de ID de package observados; addon-google_apis-google-19 'em localization inconsistente
  • 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.