【问题标题】:Volley library getting out of memory errorVolley 库内存不足错误
【发布时间】:2016-07-05 14:34:08
【问题描述】:
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)

【问题讨论】:

  • 这主要是因为我们不知道当我们使用大数据或文件静态加载时,应用程序可以加载多大的数据或文件,这会导致某些没有足够内存的设备出现错误。 . 尝试使用小尺寸文件..

标签: android android-volley


【解决方案1】:

我找到了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 memoryOOM 错误。

更多参考或信息链接:
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

【讨论】:

    【解决方案2】:

    如果还有人遇到这个问题,那就was fixed on volley version 1.1.0-rc1

    只需升级到这个版本。

    【讨论】:

      【解决方案3】:

      当您尝试将超过可用 RAM 的大数据加载到应用程序的内存中时,会发生 OutOfMemoryError。我不知道您目前使用 volley 做什么,但请尝试禁用 volley see here 的缓存或不立即加载大文件或尝试压缩文件。如果您提供有关您要实现的目标以及导致错误的代码部分的更多详细信息,将会有所帮助。

      【讨论】:

        【解决方案4】:

        这是在尝试读取标题时抛出的,该标题应该比较小。另请注意,DiskBasedCache 大小仅为 5MB。

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

        【讨论】:

          猜你喜欢
          • 2017-04-14
          • 1970-01-01
          • 1970-01-01
          • 2014-07-28
          • 2018-05-05
          • 2011-08-18
          • 1970-01-01
          • 2015-09-20
          相关资源
          最近更新 更多