线程池内存不足异常

发布于 2024-10-07 10:04:16 字数 1327 浏览 5 评论 0原文

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

  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);
            }
        }
    }

感谢您的帮助

I keep getting an out of memory exception on the following code and i was wondering if there was something i could do to stop this from happening.

  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);
            }
        }
    }

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-10-14 10:04:16

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

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

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

更新

来自我的评论

您正在序列化对 DoWork 中所有代码的访问,因此使用多个线程是没有意义的。

类似下面的内容将避免从 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..");
        }
    }

You are removing from the beginning of the list which will cause a new list to be generated and the old one copied into it. This will cause havoc with the Large Object Heap given that you are dealing with lists with a large number of elements.

If you must use this type of design remove items in a reverse direction, this will prevent copying of the underlying array of the List i.e. remove from the end towards the beginning.

A better design would be to use a counter that you increment using Interlocked.Increment and use this to access members in your list. You can do this safely as you are not changing your list after you have created it.

Updated

From my comment

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

Something like the following will avoid the issue with removing from your ID list, plus allow you to potentially retrieve items concurrently from your repository and so utilize those extra threads. This would have to me measured though - adding threads is not a guarantee of performance improvement.

Also if your "_repository" is a collection, ensure that you size it to the same size as your ID list size. This will prevent a lot of intermediate array copying as the collection grows as you add items.

    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..");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文