创建大量对象时内存不足 C#

时间:2023-02-16
本文介绍了创建大量对象时内存不足 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中处理 100 万条记录,这些记录是从 MySQL 数据库中检索到的.为此,我使用 Linq 获取记录并使用 .Skip() 和 .Take() 一次处理 250 条记录.对于每个检索到的记录,我需要创建 0 到 4 个项目,然后将其添加到数据库中.因此,必须创建的 Item 总数平均约为 200 万个.

I'm processing 1 million records in my application, which I retrieve from a MySQL database. To do so I'm using Linq to get the records and use .Skip() and .Take() to process 250 records at a time. For each retrieved record I need to create 0 to 4 Items, which I then add to the database. So the average amount of total Items that has to be created is around 2 million.

IQueryable<Object> objectCollection = dataContext.Repository<Object>();
int amountToSkip = 0;
IList<Object> objects = objectCollection.Skip(amountToSkip).Take(250).ToList();
while (objects.Count != 0)
        {
            using (dataContext = new LinqToSqlContext(new DataContext()))
            {
                foreach (Object objectRecord in objects)
                {
                    // Create 0 - 4 Random Items
                    for (int i = 0; i < Random.Next(0, 4); i++)
                    {
                        Item item = new Item();
                        item.Id = Guid.NewGuid();
                        item.Object = objectRecord.Id;
                        item.Created = DateTime.Now;
                        item.Changed = DateTime.Now;
                        dataContext.InsertOnSubmit(item);
                    }
                }
                dataContext.SubmitChanges();
            }
            amountToSkip += 250;
            objects = objectCollection.Skip(amountToSkip).Take(250).ToList();
        }

现在创建项目时出现问题.运行应用程序时(甚至不使用 dataContext),内存会持续增加.就好像这些物品永远不会被处理掉一样.有没有人注意到我做错了什么?

Now the problem arises when creating the Items. When running the application (and not even using dataContext) the memory increases consistently. It's like the items are never getting disposed. Does anyone notice what I'm doing wrong?

提前致谢!

推荐答案

好的,我刚刚和我的一位同事讨论了这种情况,我们得出了以下有效的解决方案!

Ok I've just discussed this situation with a colleague of mine and we've come to the following solution which works!

int amountToSkip = 0;
var finished = false;
while (!finished)
{
      using (var dataContext = new LinqToSqlContext(new DataContext()))
      {
           var objects = dataContext.Repository<Object>().Skip(amountToSkip).Take(250).ToList();
           if (objects.Count == 0)
                finished = true;
           else
           {
                foreach (Object object in objects)
                {
                    // Create 0 - 4 Random Items
                    for (int i = 0; i < Random.Next(0, 4); i++)
                    {
                        Item item = new Item();
                        item.Id = Guid.NewGuid();
                        item.Object = object.Id;
                        item.Created = DateTime.Now;
                        item.Changed = DateTime.Now;
                        dataContext.InsertOnSubmit(item);
                     }
                 }
                 dataContext.SubmitChanges();
            }
            // Cumulate amountToSkip with processAmount so we don't go over the same Items again
            amountToSkip += processAmount;
        }
}

通过这个实现,我们每次都会处理 Skip() 和 Take() 缓存,因此不会泄漏内存!

With this implementation we dispose the Skip() and Take() cache everytime and thus don't leak memory!

这篇关于创建大量对象时内存不足 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:LINQ to SQL ForeignKeyReferenceAlreadyHasValueException 错误 下一篇:LINQ - 向结果添加属性

相关文章

最新文章