Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating big amount of Sitecore Items causes memory leak

I have a Sitecore application where I need to create a large number of Sitecore items in a loop from another system. Below is my testing purpose loop;

using (new Sitecore.SecurityModel.SecurityDisabler())
{
    for (int i = 0; i < 20000; i++)
    {
        Item newItem = MyTemplateItem.CreateItemFrom(DateTime.Now.Ticks.ToString(), myParentItem);

        newItem.Editing.BeginEdit();
        newItem.Fields["Title"].Value = Guid.NewGuid().ToString();
        newItem.Editing.EndEdit();
    }
}

When this loop is running and while looking in task manager, the process memory usage is getting increased with the time.

Sitecore Item class has not implemented IDisposable interface, so I cannot call the Finalizer of the created item.

How can I avoid this memory leak?

PS: I'm using a windows application to perform this operation to bypass the IIS process memory leak where Sitecore does its cache updating and index building which should be done at the end of this process.

like image 203
Sandun Perera Avatar asked May 17 '13 05:05

Sandun Perera


1 Answers

You can temporarily disable the database cache during the item creation:

using (new Sitecore.Data.DatabaseCacheDisabler())

You can disable indexing during the creation using:

Sitecore.Configuration.Settings.Indexing.Enabled = false;

Try to add those two things and see if it helps.

UPDATE: Maybe this will work. Add this inside your loop after EndEdit():

newItem = null;

if (i % 100 == 0)
{
  GC.Collect();
  GC.WaitForPendingFinalizers();
}

It will force the garbage collector to try reclaim unused memory every time 100 items have been added.

If this works then you don't really have to call the GC yourself, it will happen automatically at some point, you just don't know when.

like image 67
Ruud van Falier Avatar answered Oct 07 '22 08:10

Ruud van Falier