public string SavePath { get; set; } = @"I:\files\";

public void DownloadList(List 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);

I observed the usage of RAM for the project keeps growing

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?

Update 1

After changed to the new code

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();

}

}

I still observed increasing memory use after keeping running for 9 hours, it is much slowly growing usage though.

Just wondering, is it because that I didn't free the memory use of theTrack file?

Btw, I use ALT package for update file metadata, unfortunately, it doesn't implement IDisposable interface.

解决方案

Use WebClient.DownloadFile() to download directly to a file so you don't have the whole file in memory.

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐