Volley库出现内存不足错误

3
java.lang.OutOfMemoryError: Failed to allocate a 1465140570 byte allocation with 1622964 free bytes and 509MB until OOM
    at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:316)
    at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:526)
    at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:548)
    at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:392)
    at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:155)
    at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:85)

这主要是因为我们不知道应用程序可以加载多大的数据或文件,当我们使用大量数据或文件进行静态加载时,会导致一些设备出现内存不足的错误。因此,请尝试使用较小的文件大小。 - Uttam Panchasara
4个回答

10
我找到了这篇帮助文章:this,我正在测试它,测试完成后,我会回来写一下是否有效。

更新

好的,我用一个无限循环进行了测试。

像这样:

//global var 
Boolean running = true;
Thread MyThread = new Thread(){//create thread
    @Override
    public void run() {
    RequestQueue queue = Volley.newRequestQueue(MyActivity.this);// start one time no more...
    String url = "http://192.168.1.78/testget.php?key=123456789"; // my URL

    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {//where request return ...
                @Override
                public void onResponse(String response) {
                    System.out.println("Volley response: \"" + response +"\"");
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(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(
            new DefaultRetryPolicy(
                    20000,//time to wait for it in this case 20s
                    20,//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;

我测试了它 130144 次 ~ 18.07 小时,没有任何错误。
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

调用newRequestQueue多次出现错误

测试使用RequestQueue queue = Volley.newRequestQueue(MyActivity.this);调用多次

示例:

//global var 
Boolean running = true;

Thread MyThread = new Thread(){//create thread

    void getSomethingFromSite(){
        
        RequestQueue queue = Volley.newRequestQueue(MyActivity.this);//calling request more than one time... error you not call more than one time
        String url = "http://192.168.1.78/testget.php?key=123456789"; // my url

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {//where request return ...
                @Override
                public void onResponse(String response) {
                    System.out.println("Volley response: \"" + response +"\"");
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(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(
                new DefaultRetryPolicy(
                        20000,//time to wait for it in this case 20s
                        20,//tries in case of error
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
                )
        );
        // Add the request to the RequestQueue.
        queue.add(stringRequest);
    }

    @Override
    public void run() {
            
        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;

错误475次。
I/System.out: Counter: 471
I/System.out: Volley response: "Something"
W/libc: pthread_create failed: couldn't allocate 1064960-byte stack: Out of memory
E/art: Throwing OutOfMemoryError "pthread_create (1040KB stack) failed: Try again"
       --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: Thread-373
  Process: com.masm.testservice, PID: 3821
  java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
      at java.lang.Thread.nativeCreate(Native Method)
      at java.lang.Thread.start(Thread.java:1063)
      at com.android.volley.RequestQueue.start(RequestQueue.java:152)
      at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:66)
      at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
      at com.masm.testservice.MyService$1.getDataVolley(MyService.java:84)
      at com.masm.testservice.MyService$1.run(MyService.java:129)
E/AndroidRuntime: Error reporting crash
    java.lang.OutOfMemoryError
        at android.os.Parcel.nativeWriteString(Native Method)
        at android.os.Parcel.writeString(Parcel.java:536)
        at android.app.ApplicationErrorReport$CrashInfo.writeToParcel(ApplicationErrorReport.java:389)
        at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4099)
        at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

结论

不要多次调用newRequestQueue方法,否则您将会得到out memory或者OOM错误。

更多相关参考或者信息:
1.- 使用volley库时出现内存不足错误
2.- 我使用大量数据进行请求后,Volley给我带来了内存不足的异常
3.- Volley运行缓慢并导致内存泄漏
4.- Volley框架请求在内存中保留对象


2
如果有人仍然遇到这个问题,它在 volley 版本 1.1.0-rc1 中已经被修复了。只需要升级到这个版本即可。

1

当您尝试将超出可用RAM的大型数据加载到应用程序的内存中时,会发生OutOfMemoryError。我不知道您当前正在使用volley做什么,但请尝试为volley禁用缓存在此处查看,或者不要一次性加载大文件,或者尝试压缩文件。如果您提供更多有关您尝试实现的内容以及导致错误的代码部分的详细信息,会更有帮助。


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接