C# Parallel.ForEach() 内存使用量不断增长 [英] C# Parallel.ForEach() memory usage keeps growing

查看:32
本文介绍了C# Parallel.ForEach() 内存使用量不断增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public string SavePath { get;放;} = @"我:文件";public void DownloadList(List list){var rest = ExcludeDownloaded(list);var 结果 = Parallel.ForEach(rest, link=>{下载链接);});}私人无效下载(字符串链接){使用(var net = new System.Net.WebClient()){var data = net.DownloadData(link);var fileName = 生成唯一文件名的代码;如果(文件.存在(文件名))返回;File.WriteAllBytes(fileName, data);}}var downloader = new DownloaderService();var links = downloader.GetLinks();downloader.DownloadList(links);

我观察到项目的 RAM 使用量不断增长

我猜 Parallel.ForEach() 有问题,但我想不通.

是否存在内存泄漏,或者发生了什么?

<小时>

更新 1

改成新代码后

private void Download(string link){使用(var net = new System.Net.WebClient()){var fileName = 生成唯一文件名的代码;如果(文件.存在(文件名))返回;var data = net.DownloadFile(link, fileName);Track theTrack = new Track(fileName);theTrack.Title = GetCDName();theTrack.Save();}}

在持续运行 9 小时后,我仍然观察到内存使用量增加,但使用量增长缓慢.

只是想知道,是不是因为我没有释放Track文件的内存使用?

顺便说一句,我使用 ALT 包来更新文件不幸的是,元数据没有实现 IDisposable 接口.

解决方案

使用 WebClient.DownloadFile() 直接下载到一个文件,这样你就不会在内存中保存整个文件.

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

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.

这篇关于C# Parallel.ForEach() 内存使用量不断增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆