C# Parallel.ForEach() 内存使用量不断增长 [英] C# Parallel.ForEach() memory usage keeps growing
问题描述
public string SavePath { get; set; } = @"I:files";
public void DownloadList(List<string> list)
{
var rest = ExcludeDownloaded(list);
var result = Parallel.ForEach(rest, link=>
{
Download(link);
});
}
private void Download(string link)
{
using(var net = new System.Net.WebClient())
{
var data = net.DownloadData(link);
var fileName = code to generate unique fileName;
if (File.Exists(fileName))
return;
File.WriteAllBytes(fileName, data);
}
}
var downloader = new DownloaderService();
var links = downloader.GetLinks();
downloader.DownloadList(links);
我观察到项目的 RAM 使用量不断增长
I observed the usage of RAM for the project keeps growing
我猜 Parallel.ForEach() 有问题,但我想不通.
I guess there is something wrong on the Parallel.ForEach(), but I cannot figure it out.
是否存在内存泄漏,或者发生了什么?
Is there the memory leak, or what is happening?
更新 1
改成新代码后
private void Download(string link)
{
using(var net = new System.Net.WebClient())
{
var fileName = code to generate unique fileName;
if (File.Exists(fileName))
return;
var data = net.DownloadFile(link, fileName);
Track theTrack = new Track(fileName);
theTrack.Title = GetCDName();
theTrack.Save();
}
}
在持续运行 9 小时后,我仍然观察到内存使用量增加,但使用量增长缓慢.
I still observed increasing memory use after keeping running for 9 hours, it is much slowly growing usage though.
只是想知道,是不是因为我没有释放Track文件的内存使用?
Just wondering, is it because that I didn't free the memory use of theTrack file?
顺便说一句,我使用 ALT 包来更新文件不幸的是,元数据没有实现 IDisposable 接口.
Btw, I use ALT package for update file metadata, unfortunately, it doesn't implement IDisposable interface.
推荐答案
使用 WebClient.DownloadFile()
直接下载到一个文件,这样你就不会在内存中保存整个文件.
Use WebClient.DownloadFile()
to download directly to a file so you don't have the whole file in memory.
这篇关于C# Parallel.ForEach() 内存使用量不断增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!