为什么调用Volley.RequestQuery.add()时出现OutOfMemory异常?
Why there is OutOfMemory exception while calling Volley.RequestQuery.add()?
当我删除 requestQuery.add() 行时 - 除了缺少网络数据外,一切正常。添加 requestQuery.add() 后 - 我得到这个:
回溯:
08-29 18:15:53.071 6305-7075/? E/dalvikvm-heap﹕ Out of memory on a 840969348-byte allocation.
08-29 18:15:53.141 6305-7075/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-27227
java.lang.OutOfMemoryError
at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:322)
at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:540)
at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:562)
at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:403)
at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:156)
at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84)
这是我的代码:
public class DataReceiver {
public static void initializeCountriesListUpdate(String urlString, final Context context) throws IOException {
RequestQueue requestQueue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlString,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
ArrayList<Country> countriesList = JsonParser.getCountriesFromJson(response, context);
((CountriesListActivity)context).updateCountriesList(countriesList);
((CountriesListActivity)context).progressDialog.hide();
((CountriesListActivity)context).progressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("NetworkManager", "Cannot resolve data from the server");
}
});
VolleySingleton.getInstance().getRequestQueue().add(jsonObjectRequest);
}
UPD1:
我尝试添加
android:hardwareAccelerated="false"
android:largeHeap="true"
到 Manifest,但没有帮助。
位图经常导致内存不足错误。
尝试省略位图加载以测试这是否是问题所在。
检查那里的大小,并可能使用这里描述的 calculateInSampleSize http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
我怀疑您的主要问题是内存异常,作为副作用,您在已经遇到 OutOfMemoryError 异常(这是 "has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41dbc578 V.E..... R......D 0,0-684,192} that was originally added here"部分)。
主要异常表明您的缓存可能比堆大,并且您来自服务器的响应非常大,您应该集中精力解决这个问题。
我认为这个 question 可能会帮助您解决这个问题。
当我删除 requestQuery.add() 行时 - 除了缺少网络数据外,一切正常。添加 requestQuery.add() 后 - 我得到这个:
回溯:
08-29 18:15:53.071 6305-7075/? E/dalvikvm-heap﹕ Out of memory on a 840969348-byte allocation.
08-29 18:15:53.141 6305-7075/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-27227
java.lang.OutOfMemoryError
at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:322)
at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:540)
at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:562)
at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:403)
at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:156)
at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84)
这是我的代码:
public class DataReceiver {
public static void initializeCountriesListUpdate(String urlString, final Context context) throws IOException {
RequestQueue requestQueue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlString,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
ArrayList<Country> countriesList = JsonParser.getCountriesFromJson(response, context);
((CountriesListActivity)context).updateCountriesList(countriesList);
((CountriesListActivity)context).progressDialog.hide();
((CountriesListActivity)context).progressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("NetworkManager", "Cannot resolve data from the server");
}
});
VolleySingleton.getInstance().getRequestQueue().add(jsonObjectRequest);
}
UPD1:
我尝试添加
android:hardwareAccelerated="false"
android:largeHeap="true"
到 Manifest,但没有帮助。
位图经常导致内存不足错误。 尝试省略位图加载以测试这是否是问题所在。 检查那里的大小,并可能使用这里描述的 calculateInSampleSize http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
我怀疑您的主要问题是内存异常,作为副作用,您在已经遇到 OutOfMemoryError 异常(这是 "has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41dbc578 V.E..... R......D 0,0-684,192} that was originally added here"部分)。
主要异常表明您的缓存可能比堆大,并且您来自服务器的响应非常大,您应该集中精力解决这个问题。
我认为这个 question 可能会帮助您解决这个问题。