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

后端 未结 3 1992
一个人的身影
一个人的身影 2020-12-09 09:15

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:

         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-09 09:19

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

提交回复
热议问题