c#-3.0 - Parallel.For System.OutOfMemoryException

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

我们有一个相当简单的程序,用于创建备份。我正在尝试并行化它,但在 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 个工作线程。

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

最佳答案

这里做出一些猜测,因为我还没有从您的问题的评论中得到反馈。

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

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

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

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

更新

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

ParallelOptions options = new ParallelOptions { MaxDegreeOfParallelism = 2 };

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

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

关于c#-3.0 - Parallel.For System.OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2985842/

相关文章:

C# 自动属性

c# - 需要实现 MessageHeaders.WriteHeaderContents 的示例

c++ - C# 中的 inet_addr 函数等效于什么

python - 调试并行 Python 程序 (mpi4py)

C# Parallel - 将项目添加到正在迭代的集合中,或等效?

Python - 使用 joblib 进行循环并行化

java - 游戏开发时位图大小超过VM预算

java - 是否有一个 Java 库可以根据数据的大小将数据缓存在内存或磁盘中?

java - 如何克服大文件写入期间的 OutOfMemoryError

asp.net-mvc - .NET MVC : How to implement different page appearance per user?