我找到了这篇帮助文章:
this,我正在测试它,测试完成后,我会回来写一下是否有效。
更新
好的,我用一个无限循环进行了测试。
像这样:
Boolean running = true;
Thread MyThread = new Thread(){
@Override
public void run() {
RequestQueue queue = Volley.newRequestQueue(MyActivity.this);
String url = "http://192.168.1.78/testget.php?key=123456789";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@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());
}
});
stringRequest.setShouldCache(false);
stringRequest.setRetryPolicy(
new DefaultRetryPolicy(
20000,
20,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
);
int i=0;
while(running){
System.out.println("counter: "+i);
i++;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Sleep interrupted");
}
queue.add(stringRequest);
}
System.out.println("onEnd Thread");
}
};
MyThread.start();
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);
调用多次
示例:
Boolean running = true;
Thread MyThread = new Thread(){
void getSomethingFromSite(){
RequestQueue queue = Volley.newRequestQueue(MyActivity.this);
String url = "http://192.168.1.78/testget.php?key=123456789";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@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());
}
});
stringRequest.setShouldCache(false);
stringRequest.setRetryPolicy(
new DefaultRetryPolicy(
20000,
20,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
);
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) {
System.out.println("Sleep interrupted");
}
getSomethingFromSite();
}
System.out.println("onEnd Thread");
}
};
MyThread.start();
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框架请求在内存中保留对象