c# - 线程池内存不足异常

标签 c# .net threadpool out-of-memory

我在以下代码中不断遇到内存不足异常,我想知道是否可以采取一些措施来阻止这种情况的发生。

  private static List<string> MyIds { get; set; }
  private static object LockObject { get; set; }
  private static int Counter { get; set; }
  private static readonly NumOfThreads = 5;

  static void Main(string[] args)
  {
      try
      {
          Console.Clear();
          LockObject = new object();
          // Pull id's into memory (A list of around 1 million ids)
          MyIds = _repository.GetIds();
          for (int i = 0; i < NumOfThreads ; i++)
                ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), (object)i);

       }
       catch (Exception ex)
       {
           Console.WriteLine(ex.StackTrace);
       }
   }

   public static void DoWork(Object stateInfo)
   {
        while (MyList.Count > 0)
        {
            lock (LockObject)
            {
                if (MyList.Count == 0)
              return;

             string id = MyList[0];

          var record = _repository.GetRecord(id);
             _repository.Add(record);

          Counter++;
          if (Counter % 100 == 0)
                            System.Console.WriteLine(DateTime.Now + " - Imported " + Counter.ToString() + " Records..");

                MyList.RemoveAt(0);
            }
        }
    }

感谢您的帮助

最佳答案

您正在从列表的开头删除,这将导致生成新列表并将旧列表复制到其中。这将对 Large Object Heap 造成严重破坏鉴于您正在处理包含大量元素的列表。

如果您必须使用这种类型的设计以相反的方向删除项目,这将防止复制 List 的底层数组,即从末尾向开头删除。

更好的设计是使用您使用 Interlocked.Increment 递增的计数器,并使用它来访问列表中的成员。您可以安全地执行此操作,因为您在创建列表后不会更改它。

已更新

来 self 的评论

You are serializing access to all code in DoWork so there's no point in using multiple threads.

类似下面的东西将避免从您的 ID 列表中删除的问题,并且允许您潜在地从您的存储库中同时检索项目,从而利用这些额外的线程。不过,这对我来说必须衡量 - 添加线程并不能保证性能提高。

此外,如果您的“_repository”是一个集合,请确保将其调整为与您的 ID 列表大小相同的大小。随着集合随着项目的添加而增长,这将防止大量中间数组复制。

    private static int _counter = -1;

    public static void DoWork(Object stateInfo)
    {
        int index;

        while ((index = Interlocked.Increment(ref _counter)) < MyList.Count)
        {
            string id = MyList[index];

            var record = _repository.GetRecord(id);

            lock (LockObject)
            {                    
                _repository.Add(record);
            }

            if (index % 100 == 0)
                Console.WriteLine(DateTime.Now + " - Imported " + (index + 1) + " Records..");
        }
    }

关于c# - 线程池内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4433581/

相关文章:

java - 线程池的工作方式不可预测

c# - NHibernate Web 服务中的 "Could not execute query error"

c# - asp.net文件上传

C#多线程文件IO(阅读)

c# - 从 Controller 下载文件 RedirectToAction()

c# - 如何在 C# 中创建一个 List<int> 数组?

java - 修复了线程池线程阻塞,当提交了足够多的任务时

C# 继承挑战

c# - submit 和 beginform 的相关性

c# - Fluent NHibernate - ProjectionList - ICriteria 返回空值