Skip to content Skip to sidebar Skip to footer

Volley Library Getting Out Of Memory Error

java.lang.OutOfMemoryError: Failed to allocate a 1465140570 byte allocation with 1622964 free bytes and 509MB until OOM at com.android.volley.toolbox.DiskBasedCache.streamToByt

Solution 1:

I found this help, I'm testing It, when I finish, I will come here to write if It works


update

Well I tested It with an infinite cycle

like this

//global var Booleanrunning=true;
ThreadMyThread=newThread(){//create thread@Overridepublicvoidrun() {
    RequestQueuequeue= Volley.newRequestQueue(MyActivity.this);// start one time no more...Stringurl="http://192.168.1.78/testget.php?key=123456789"; // my URL// Request a string response from the provided URL.StringRequeststringRequest=newStringRequest(Request.Method.GET, url,
            newResponse.Listener<String>() {//where request return ...@OverridepublicvoidonResponse(String response) {
                    System.out.println("Volley response: \"" + response +"\"");
                }
            }, newResponse.ErrorListener() {
        @OverridepublicvoidonErrorResponse(VolleyError error) {
            System.out.println("There is an error: "+error.getMessage()); // on error, ex: URL bad... internet down, etc..
        }
    });
    stringRequest.setShouldCache(false);// no caching url...
    stringRequest.setRetryPolicy(
            newDefaultRetryPolicy(
                    20000,//time to wait for it in this case 20s20,//tries in case of error
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
            )
    );

        
        int i=0;
        while(running){
            
            System.out.println("counter: "+i);
            i++;
            
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                //e.printStackTrace();
                System.out.println("Sleep interrupted");
            }
            
            // Add the request to the RequestQueue.
            queue.add(stringRequest);
        }
        System.out.println("onEnd Thread");
    }
};
MyThread.start(); // start thread//in some button declare 
running=false;

I tested it 130144 times ~ 18.07 hr, without any error

System.out: Counter: 130141
System.out: Volley response: "something"
System.out: Counter: 130142
System.out: Volley response: "something"
System.out: Counter: 130143
System.out: Volley response: "something"
System.out: Counter: 130144

Getting Error with call newRequestQueue more than one time

Test with RequestQueue queue = Volley.newRequestQueue(MyActivity.this); calling more than one time

Example:

//global var Boolean running = true;

ThreadMyThread = newThread(){//create threadvoidgetSomethingFromSite(){
        
        RequestQueue queue = Volley.newRequestQueue(MyActivity.this);//calling request more than one time... error you not call more than one timeString url = "http://192.168.1.78/testget.php?key=123456789"; // my url// Request a string response from the provided URL.StringRequest stringRequest = newStringRequest(Request.Method.GET, url,
            newResponse.Listener<String>() {//where request return ...@OverridepublicvoidonResponse(String response) {
                    System.out.println("Volley response: \"" + response +"\"");
                }
            }, newResponse.ErrorListener() {
                @OverridepublicvoidonErrorResponse(VolleyError error) {
                    System.out.println("There is an error: "+error.getMessage()); // on error, ex: URL bad... internet down, etc..
            }
        });
        stringRequest.setShouldCache(false);// no caching url...
        stringRequest.setRetryPolicy(
                newDefaultRetryPolicy(
                        20000,//time to wait for it in this case 20s20,//tries in case of errorDefaultRetryPolicy.DEFAULT_BACKOFF_MULT
                )
        );
        // Add the request to the RequestQueue.
        queue.add(stringRequest);
    }

    @Overridepublicvoidrun() {
            
        int i=0;
        while(running){
            
            System.out.println("counter: "+i);
            i++;
            
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                //e.printStackTrace();System.out.println("Sleep interrupted");
            }
            //call volley getSomethingFromSite();
        }
        System.out.println("onEnd Thread");
    }
};
MyThread.start(); // start thread//in some button declare 
running false;

Erro with 475 times

I/System.out: Counter:471I/System.out: Volley response:"Something"W/libc: pthread_create failed:couldn'tallocate 1064960-byte stack:OutofmemoryE/art:ThrowingOutOfMemoryError"pthread_create (1040KB stack) failed: Try again"---------beginningofcrashE/AndroidRuntime: FATAL EXCEPTION:Thread-373Process:com.masm.testservice,PID:3821java.lang.OutOfMemoryError:pthread_create(1040KBstack)failed:Tryagainatjava.lang.Thread.nativeCreate(NativeMethod)atjava.lang.Thread.start(Thread.java:1063)atcom.android.volley.RequestQueue.start(RequestQueue.java:152)atcom.android.volley.toolbox.Volley.newRequestQueue(Volley.java:66)atcom.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)atcom.masm.testservice.MyService$1.getDataVolley(MyService.java:84)atcom.masm.testservice.MyService$1.run(MyService.java:129)E/AndroidRuntime:Errorreportingcrashjava.lang.OutOfMemoryErroratandroid.os.Parcel.nativeWriteString(NativeMethod)atandroid.os.Parcel.writeString(Parcel.java:536)atandroid.app.ApplicationErrorReport$CrashInfo.writeToParcel(ApplicationErrorReport.java:389)atandroid.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4099)atcom.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)atjava.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)atjava.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

Conclusion

You do not call newRequestQueue more than one time... you will get error of out memory or OOM error.

More links for reference or information: 1.- Out of memory error when use volley library 2.- Volley give me Out of memory exception after I make a lot of request with big amount of data 3.- Volley slow and causing memory leak 4.- Volley framewok request keeps objects in memory

Solution 2:

If anyone still encounters this issue, it was fixed on volley version 1.1.0-rc1

Just upgrade to this version.

Solution 3:

OutOfMemoryError occurs when you try to load large data into your app's memory that exceeds the available RAM. I do not know what you are using volley for at the moment but try disabling cache for volley see here or not load a huge file at once or try compressing the file. It would help if you give more detail on what you are trying to achieve and the code section that is causing the error.

Solution 4:

This is thrown while trying to read the header, which should be comparatively small. Note as well that the DiskBasedCache size is only 5MB.

See https://stackoverflow.com/a/42196956/901597

Post a Comment for "Volley Library Getting Out Of Memory Error"