在我的项目中,我使用volley来下载一个JSON流,我将其解析并显示在列表视图中.我使用以下方法加载我的数据:
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 one rrorResponse(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);}
对此方法的第一次调用非常有效.在第二次调用中,我收到超时错误.当我使用以下命令时:
jr.setRetryPolicy(new DefaultRetryPolicy( 2500, DefaultRetryPolicy.DEFAulT_TIMEOUT_MS, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));
为了增加请求的时间,请求超过30秒并产生以下日志输出:
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 56ms10-19 20:53:19.796: D/dalvikvm(17523): WAIT_FOR_CONCURRENT_GC blocked 51ms10-19 20:53:19.826: I/dalvikvm-heap(17523): Grow heap (frag case) to 35.123MB for 10935708-byte allocation10-19 20:53:19.857: D/dalvikvm(17523): GC_FOR_ALLOC freed 3K, 20% free 35100K/43680K, paused 23ms, total 28ms10-19 20:53:19.917: D/dalvikvm(17523): GC_CONCURRENT freed 2018K, 19% free 35816K/43680K, paused 3ms+4ms, total 60ms10-19 20:53:20.007: D/dalvikvm(17523): GC_CONCURRENT freed 4874K, 15% free 37226K/43680K, paused 2ms+3ms, total 27ms10-19 20:53:20.007: D/dalvikvm(17523): WAIT_FOR_CONCURRENT_GC blocked 24ms10-19 20:53:20.067: D/dalvikvm(17523): GC_FOR_ALLOC freed 5037K, 15% free 38601K/44900K, paused 19ms, total 19ms10-19 20:53:20.117: D/dalvikvm(17523): GC_FOR_ALLOC freed 4680K, 14% free 40045K/46564K, paused 20ms, total 20ms10-19 20:53:20.177: D/dalvikvm(17523): GC_FOR_ALLOC freed 5576K, 14% free 41572K/48272K, paused 20ms, total 20ms10-19 20:53:20.227: D/dalvikvm(17523): GC_FOR_ALLOC freed 6133K, 15% free 43406K/50548K, paused 20ms, total 20ms10-19 20:53:20.287: D/dalvikvm(17523): GC_CONCURRENT freed 6486K, 15% free 45029K/52428K, paused 2ms+2ms, total 24ms10-19 20:53:20.287: D/dalvikvm(17523): WAIT_FOR_CONCURRENT_GC blocked 11ms10-19 20:53:20.407: D/Volley(17523): [1] Request.finish: 42553 ms: [ ] http://****** 0x63ea5535 norMAL 1
当我在浏览器中执行相同的请求时,只需几秒钟.为什么延迟和令人难以置信的内存消耗?
解决方法:
每次调用该方法都会创建一个新的RequestQueue,这不是推荐的方法.您应该创建一个RequestQueue,可能是一个公开可见的单例,在创建应用程序时初始化一次.
尝试将RequestQueue移到外面,看看它是否解决了您的问题.
总结以上是内存溢出为你收集整理的android – Volley缓慢而导致内存泄漏全部内容,希望文章能够帮你解决android – Volley缓慢而导致内存泄漏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)