Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Jun 30, 2020 9:19 am

Hello,

several of our clients reporting System.OutofMemoryException during mail merge. After investigation I found that the following code leads to memory leak and exponentially increasing process time:

Code: Select all
using (var document = new Spire.Doc.Document())
{
    document.LoadFromFile(template);
    document.MailMerge.ClearFields = true;

    foreach (var mrw in data)
    {
        var mergedFileName = TempFileManager.GetTempFileName(".docx", false);

        using (var d = document.Clone())
        {
            document.CloneWebSettingsTo(d);

            //d.MailMerge.Execute(mrw.RawData);
            //d.IsUpdateFields = false;

            d.ViewSetup.DocumentViewType = DocumentViewType.PrintLayout;
            d.SaveToFile(mergedFileName);

            var plainText = d.GetText();
        }
    }
}


Here is the time for the first 20 documents:

51 ms
36 ms
34 ms
36 ms
35 ms
37 ms
43 ms
50 ms
68 ms
101 ms
183 ms
297 ms
634 ms
1155 ms
2382 ms
4603 ms
9170 ms
19321 ms
37272 ms
84950 ms

Spire.Doc version - 8.4.7.4046. The same behavior with the latest 8.5.4.4046.

The memory also increase extremely.

If I modify the code in the following way everything is fine but for performance reasons I think there is no scene to load the template every time:

Code: Select all
foreach (var mrw in data)
{
    using (var d = new Spire.Doc.Document())
    {
        d.LoadFromFile(template);
        d.MailMerge.ClearFields = true;

        var mergedFileName = TempFileManager.GetTempFileName(".docx", false);

        //d.MailMerge.Execute(mrw.RawData);
        //d.IsUpdateFields = false;

         d.ViewSetup.DocumentViewType = DocumentViewType.PrintLayout;
         d.SaveToFile(mergedFileName);

         var plainText = d.GetText();
        }
}


EDITED - I found the problematic part of code - document.CloneWebSettingsTo(d);

I introduced this after suggestion by you in my previous post - https://www.e-iceblue.com/forum/embedded-png-images-are-converted-to-gif-with-lower-bitness-t9134.html.

profiler007
 
Posts: 72
Joined: Wed Nov 13, 2019 11:32 am

Tue Jun 30, 2020 11:00 am

Hello,

Thanks for your inquiry.
I simulated a template file and tested your scenario and noticed that when the loop was executed about 10 times, the time spent and memory usage increased extremely. This issue has been logged in our bug tracking system with the ticket SPIREDOC-4712 for further investigation. If there is any update, we will inform you.
Sorry for the inconvenience caused.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc