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.