凌空慢,导致内存泄漏 [英] Volley slow and causing memory leak
问题描述
在我的项目,我使用抽射下载我分析,并显示在ListView一个JSON流。我用下面的方法加载我的数据:
In my project, I am using volley to download a JSON stream which I parse and show in a listview. I use the following method to load my data:
private void loadEventData(int year, final int month) {
// get volley request queue
requestQueue = cpcApplication.getRequestQueue(getActivity());
String url = "****************?year=" + year
+ "&month=" + month;
pd = ProgressDialog.show(getActivity(), "Loading Events", "Retrieving Data from Server");
pd.setCancelable(true);
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, response.toString());
// parse the incoming response
parseJson(response, month);
// notify the listview that the data set has changed
adapter.notifyDataSetChanged();
// set the listview at the top position
listView.setSelection(current.get(Calendar.DAY_OF_MONTH));
// dismiss the ProgressDialog
pd.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
// cancel the progress dialog
pd.dismiss();
// let the user know that a network connection is not available
Toast.makeText(getActivity(), "Cannot communicate with server. Check network connection.", Toast.LENGTH_LONG).show();
}
});
// add the network request to the queue
requestQueue.add(jr);
}
这种方法的第一次调用精美的作品。在第二个电话,我得到一个超时错误。当我使用下面的命令:
The first call to this method works beautifully. In the second call, I get a timeout error. When I use the following command:
jr.setRetryPolicy(new DefaultRetryPolicy(
2500, DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
要增加的时间量的要求,请求接管30秒,产生以下的日志输出:
to increase the amount of time for the request, the request takes over 30 seconds and produces the following log output:
10-19 20:53:19.746: D/Volley(17523): [2786] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://************ 0x63ea5535 NORMAL 1> [lifetime=41769], [size=5467846], [rc=200], [retryCount=2]
10-19 20:53:19.796: D/dalvikvm(17523): GC_CONCURRENT freed 7462K, 26% free 24424K/33000K, paused 6ms+4ms, total 56ms
10-19 20:53:19.796: D/dalvikvm(17523): WAIT_FOR_CONCURRENT_GC blocked 51ms
10-19 20:53:19.826: I/dalvikvm-heap(17523): Grow heap (frag case) to 35.123MB for 10935708-byte allocation
10-19 20:53:19.857: D/dalvikvm(17523): GC_FOR_ALLOC freed 3K, 20% free 35100K/43680K, paused 23ms, total 28ms
10-19 20:53:19.917: D/dalvikvm(17523): GC_CONCURRENT freed 2018K, 19% free 35816K/43680K, paused 3ms+4ms, total 60ms
10-19 20:53:20.007: D/dalvikvm(17523): GC_CONCURRENT freed 4874K, 15% free 37226K/43680K, paused 2ms+3ms, total 27ms
10-19 20:53:20.007: D/dalvikvm(17523): WAIT_FOR_CONCURRENT_GC blocked 24ms
10-19 20:53:20.067: D/dalvikvm(17523): GC_FOR_ALLOC freed 5037K, 15% free 38601K/44900K, paused 19ms, total 19ms
10-19 20:53:20.117: D/dalvikvm(17523): GC_FOR_ALLOC freed 4680K, 14% free 40045K/46564K, paused 20ms, total 20ms
10-19 20:53:20.177: D/dalvikvm(17523): GC_FOR_ALLOC freed 5576K, 14% free 41572K/48272K, paused 20ms, total 20ms
10-19 20:53:20.227: D/dalvikvm(17523): GC_FOR_ALLOC freed 6133K, 15% free 43406K/50548K, paused 20ms, total 20ms
10-19 20:53:20.287: D/dalvikvm(17523): GC_CONCURRENT freed 6486K, 15% free 45029K/52428K, paused 2ms+2ms, total 24ms
10-19 20:53:20.287: D/dalvikvm(17523): WAIT_FOR_CONCURRENT_GC blocked 11ms
10-19 20:53:20.407: D/Volley(17523): [1] Request.finish: 42553 ms: [ ] http://****** 0x63ea5535 NORMAL 1
当我在浏览器中执行相同的请求,只需要几秒钟。为什么延迟和令人难以置信的内存消耗?
When I perform the same request in a browser, it takes only several seconds. Why the delay and incredible memory consumption?
推荐答案
的方法每次调用创建一个新的请求队列
这不是一个推荐的方法。你应该创建一个请求队列
,大概在创建应用程序时初始化一次公开可见单。
Every call to the method you create a new RequestQueue
which is not a recommended approach. You should create one RequestQueue
, probably a publicly visible singleton that is initialized once when the app is created.
尝试移动请求队列
之外,看看它是否解决您的问题。
Try moving the RequestQueue
outside and see if it solves your problem.
这篇关于凌空慢,导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!