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屋!