更新:
gc 只会在最后的 } 之后工作,这意味着只有在整个 for 循环完成后,gc 垃圾收集才会运行一次。那么,这里有什么可以使用的编程模式吗?有点像 for 循环,但同时也允许 gc 运行每次迭代。我不知道我是否已经说清楚了......我可以忍受由 gc 运行每次迭代引起的性能下降。
我在一个 android 项目上使用 volley,但我无法得到一个有效的解决方案。
基本上我有大量的东西我会不断地发送(通过 http 和 volley lib)。当我发送其中的 10 个时没问题,但如果我连续发送,它最终会耗尽所有内存,并且应用程序由于占用的热量空间而终止。
下面是演示代码块
requestqueue = Common.getQueue();//it will get a normal requestqueue
for (int i = 0; i < waitinglist.size(); i++) {
synccopyreq.filedatas.add(Common.getDataFromWaitinglist(i));
Common.httpPostWithQueue( requestqueue,"tagfullcopy", httpEndPoint+"/sync/fullcopy", gson.toJson(synccopyreq), that, new Common.CallbackInterface() {
@Override
public void completed(String result)
{
Log.i("shawhu","ok done");
}
});
//this was added but it doesn't work unfortunately
//using System.gc() like so was not recommended and most of the time wont'be effective either, I knew that.
System.gc();
}
请阅读这篇文章,我已经尝试过分析器,当整个事情完成时(比如处理 10 个项目时),内存使用量会下降,但如果我连续运行它,它不会。内存使用量不断上升,最终会破坏应用程序(由android系统终止)。如何在循环中的每次迭代中释放内存?
请帮帮我,谢谢!!