【发布时间】:2012-10-24 18:13:17
【问题描述】:
我曾经有:
using (MyWebClient client = new MyWebClient(TimeoutInSeconds))
{
var res = client.DownloadData(par.Base_url);
//code that checks res
}
现在我有:
using (MyWebClient client = new MyWebClient(TimeoutInSeconds))
{
client.DownloadDataAsync(new Uri(par.Base_url));
client.DownloadDataCompleted += (sender, e) =>
{
//code that checks e.Result
}
}
其中 MyWebClient 派生自 WebClient。 现在我有很多线程在做这件事,在第一种情况下内存消耗不是问题,而在第二种情况下,我看到内存稳步上升,直到我得到 OutOfMemoryException。 我进行了分析,似乎 WebClient 是罪魁祸首,没有被处理并保留下载的数据。但为什么?两种情况有什么区别?也许 e.Result 需要以某种方式处理?
【问题讨论】:
标签: c# memory-management memory-leaks garbage-collection