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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With