【问题标题】:Parallel.For System.OutOfMemoryExceptionParallel.For System.OutOfMemoryException
【发布时间】:2011-02-28 11:23:22
【问题描述】:

我们有一个用于创建备份的相当简单的程序。我正在尝试并行化它,但在 AggregateException 中得到了 OutOfMemoryException。一些源文件夹非常大,程序在启动后大约 40 分钟内不会崩溃。我不知道从哪里开始寻找,所以下面的代码几乎是所有代码的准确转储,代码没有目录结构和异常日志记录代码。关于从哪里开始寻找的任何建议?

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace SelfBackup
{
class Program
{

static readonly string[] saSrc = { 
    "\\src\\dir1\\",
    //...
    "\\src\\dirN\\", //this folder is over 6 GB
};
static readonly string[] saDest = { 
    "\\dest\\dir1\\",
    //...
    "\\dest\\dirN\\",
};

static void Main(string[] args)
{
Parallel.For(0, saDest.Length, i =>
{
    try
    {
        if (Directory.Exists(sDest))
        {
            //Delete directory first so old stuff gets cleaned up
            Directory.Delete(sDest, true);
        }

        //recursive function 
        clsCopyDirectory.copyDirectory(saSrc[i], sDest);
    }
    catch (Exception e)
    {
        //standard error logging
        CL.EmailError();
    }
});
}
}

///////////////////////////////////////
using System.IO;
using System.Threading.Tasks;

namespace SelfBackup
{
static class clsCopyDirectory
{
    static public void copyDirectory(string Src, string Dst)
    {
        Directory.CreateDirectory(Dst);

        /* Copy all the files in the folder
           If and when .NET 4.0 is installed, change 
           Directory.GetFiles to Directory.Enumerate files for 
           slightly better performance.*/
        Parallel.ForEach<string>(Directory.GetFiles(Src), file =>
        {
            /* An exception thrown here may be arbitrarily deep into 
               this recursive function there's also a good chance that
               if one copy fails here, so too will other files in the 
               same directory, so we don't want to spam out hundreds of 
               error e-mails but we don't want to abort all together. 
               Instead, the best solution is probably to throw back up 
               to the original caller of copy directory an move on to 
               the next Src/Dst pair by not catching any possible
               exception here.*/
            File.Copy(file, //src
                      Path.Combine(Dst, Path.GetFileName(file)), //dest
                      true);//bool overwrite
        });

        //Call this function again for every directory in the folder.
        Parallel.ForEach(Directory.GetDirectories(Src), dir =>
        {
            copyDirectory(dir, Path.Combine(Dst, Path.GetFileName(dir)));
        });
    }
}

线程调试窗口显示异常时有 417 个工作线程。

编辑: 复制是从一台服务器到另一台服务器。我现在正在尝试将最后一个 Paralell.ForEach 更改为常规 foreach 来运行代码。

【问题讨论】:

  • 您通常是从磁盘 A 复制到磁盘 B,还是从同一磁盘上的一个位置复制到另一个位置?

标签: c#-3.0 parallel-processing out-of-memory .net


【解决方案1】:

在这里做一些猜测,因为我还没有对您的问题的评论提供反馈。

我猜这里发生了大量工作线程,因为操作(一个操作是在并行 foreach 上执行的工作单元)花费的时间超过了指定的时间,因此底层 ThreadPool 正在增长线程数。这将会发生,因为 ThreadPool 遵循增长池的算法,以便新任务不会被现有的长时间运行的任务阻塞,例如如果我当前的所有线程都忙了半秒钟,我将开始向池中添加更多线程。但是,如果所有任务都是长时间运行的,并且您添加的新任务会使现有任务运行时间更长,那么您将遇到麻烦。这就是为什么您可能会看到大量工作线程 - 可能是因为磁盘抖动或网络 IO 缓慢(如果涉及网络驱动器)。

我还猜测文件正在从一个磁盘复制到另一个磁盘,或者它们正在从同一个磁盘上的一个位置复制到另一个位置。在这种情况下,为问题添加线程不会有太大帮助。源磁盘和目标磁盘只有一组磁头,因此试图让它们同时执行多项操作实际上可能会减慢速度:

  • 磁头会到处乱晃。
  • 您的磁盘\操作系统缓存可能经常失效。

这对于并行化来说可能不是一个大问题。

更新

在回答您的评论时,如果您在较小的数据集上使用多个线程来加快速度,那么您可以尝试降低并行 foreach 中使用的最大线程数,例如

ParallelOptions options = new ParallelOptions { MaxDegreeOfParallelism = 2 };

Parallel.ForEach(Directory.GetFiles(Src), options, file =>
{
    //Do stuff
});

但请记住,在一般情况下,磁盘抖动可能会抵消并行化带来的任何好处。玩弄它并衡量你的结果。

【讨论】:

  • 感谢@chibacity 的洞察力。我在较小的目录(网络驱动器)上进行了一些测试,它们似乎将速度从 30 秒加快到了 5-6 秒。我希望当我将它设置在巨大的 6GB 目录上时,我会看到一半的速度。不过你的逻辑是有道理的。
  • 这种方法可能会走得更远一些,尽管在一般情况下磁盘抖动可能会抵消并行化的任何好处。我已为我的答案添加了更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 2013-05-27
相关资源
最近更新 更多