【问题标题】:EPPlus multi-threading gets "out of memory" exceptionEPPlus 多线程出现“内存不足”异常
【发布时间】:2013-04-15 06:01:27
【问题描述】:

我试图通过使用多线程来并行化 EPPlus。我尝试生成 20 个线程,每个线程生成 1000 张 10 列和 26 行的表。该程序消耗了高达 1.8GB 的​​内存并引发了“内存不足”异常。

有人知道怎么解决吗?

谢谢。

【问题讨论】:

  • 作为一个在黑暗中的镜头,你的程序是为 32 位还是 64 位编译的?
  • 它是为“任何 CPU”编译的,但我在 32 位操作系统上运行它。有什么想法吗?
  • 你能发布你的代码吗?没有足够的信息来提出其他建议。
  • 我在下面发布了我的代码。谢谢!

标签: multithreading memory out-of-memory epplus


【解决方案1】:

这是一个简单的程序,我尝试在多线程中执行,但出现“内存不足”异常:

               class Program
                {
                    private static int fileID=0;
                    static void Main(string[] args)
                    {
                        List<Thread> threadPool = new List<Thread>();
                        int threadCount = 20;


                        for(int i=0;i<threadCount;i++)
                        {
                            System.Threading.Thread reportThread = new System.Threading.Thread(() => GenerateThousandSheets());
                            threadPool.Add(reportThread);
                            reportThread.Start();
                        }

                        threadPool.ForEach(x => x.Join());
                        Console.WriteLine("Finished!");
                        Console.ReadKey();
                    }


                    private static void GenerateThousandSheets()
                    {
                        FileInfo newFile = new FileInfo(outputDir.FullName + @"\sample6.xlsx");
                        ExcelPackage pck = new ExcelPackage(newFile);

                        ExcelWorksheet ws = pck.Workbook.Worksheets[0];

                        for(int x= 0;x<1000;x++)
                        {
                            int newPosition = ws.Index + x;
                            pck.Workbook.Worksheets.Copy("Template","Sheet"+x);
                        }

                        FileInfo outputFile = new FileInfo(outputDir.FullName +fileID+ "-output.xlsx");
                        pck.SaveAs(outputFile);
                        counter++
                    }
                }

【讨论】:

    【解决方案2】:

    如果您的操作系统是 32 位,那么单个可执行文件最多可以使用 2GB。 事实上,1.8GB 确实接近这个门槛。 使用具有更多 RAM 的 64 位操作系统,或者在使用 Paralle.ForEach 使用不同的处理器亲和性时产生更少的线程或使用更少的线程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多