简体   繁体   English

C#线程Parallel.ForEach和DirectoryInfo内存不足异常

[英]C# Threading Parallel.ForEach and DirectoryInfo Out of Memory Exception

I have a very simply Parallel.ForEach that calls the code below: 我有一个非常简单的Parallel.ForEach,它调用以下代码:

var maxDegree = new ParallelOptions{MaxDegreeOfParallelism = 5};
Parallel.ForEach(PList,maxDegree,fl =>
            {
                ProjectDirectoryProcessing pjp = new ProjectDirectoryProcessing();
                pjp.ProjectProcessor(fl);
                Console.ReadLine();
            }
        );

public class ProjectDirectoryProcessing
{

    public void ProjectProcessor(string rootDirectory)
    {
           DirectoryInfo Dinfo = new DirectoryInfo(rootDirectory);
            DirectoryInfo[] directories = Dinfo.GetDirectories("*.*", SearchOption.AllDirectories);
            FileInfo[] finfo = Dinfo.GetFiles("*.*", SearchOption.AllDirectories);
            foreach (FileInfo f in finfo)
            {
                FileSize = FileSize + f.Length;
            }
            FileCount = finfo.Length;
            DirectoryCount = directories.Length;
    }
}

The problem is I run out of memory, I thought about GC.Collect() after the pjp.ProjectProcessor in the Parallel.ForEach but I'm not sure if this will work. 问题是我内存不足,我在Parallel.ForEach中的pjp.ProjectProcessor之后想到了GC.Collect(),但是我不确定这是否可以工作。 The directories are extremely large and I'm not positive that cleaning those up will help all that much. 目录非常大,我不太肯定清理这些目录会起到很大的作用。 What would be a good way to handle this? 什么是处理此问题的好方法?

Calling GC manually won't help as GC should be automatically called before throwing an OutOfMemoryException. 手动调用GC将无济于事,因为应该在引发OutOfMemoryException之前自动调用GC。 One easy solution could be to compile the application as x64. 一种简单的解决方案是将应用程序编译为x64。 Is this an option? 这是一个选择吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM