Unable to release memory while running MailMerge.Execute

I use MailMerge.Execute and I have a memory issue: the Execute method with large files reaches a peak of 1.3 GB. At the end of the process the blocked memory does not return to the initial state (about 100MB) but remains blocked at about 800MB. The memory is not released whether I use Execute(DataTable) or Execute(IMailMergeDataSource).
This is a big issue because if the program has to manage a large number of documents in a row, the committed memory tends to become increasingly larger and to saturate the resources.
What is the effective way to free the resources at the end of MailMerge.Execute?

@michele.granzo

It seems you are experiencing memory issues when using the MailMerge.Execute method with large documents. This is a known concern, as the memory usage can spike significantly during the execution of the method due to the creation of temporary internal structures required for processing.

Here are some suggestions to help manage memory more effectively:

  1. Use Temporary Files: You can specify a folder for temporary files using the SaveOptions.TempFolder property. This allows Aspose.Words to write temporary data to disk instead of keeping everything in memory, which can help reduce peak memory usage.

  2. Break Down Large Documents: If possible, consider generating smaller documents with fewer records and then combining them afterward. This approach can help manage memory usage more effectively during the merge process.

  3. Garbage Collection: After executing the MailMerge, you can force garbage collection by calling GC.Collect(). This may help in reclaiming memory that is no longer in use, although it is generally recommended to let the garbage collector manage memory automatically.

  4. Monitor Memory Usage: Keep an eye on memory usage during the execution of your application. This can help you identify specific points where memory spikes occur and adjust your approach accordingly.

If you continue to face issues, consider reaching out to Aspose support with specific details about your implementation and the documents you are working with, as they may provide additional insights or optimizations.

For further reading, you might find it helpful to look into discussions about performance and memory management in Aspose.Words, as they often contain useful tips and best practices.

Sources:
[1]: Aspose.Words hangs on MailMerge
[2]: Performance issue with mail merge and saving document to MemoryStream using .NET

@michele.granzo Document object does not keep any unmanaged resources in memory. So the memory should be released once the Document object is out of scope. If possible, please create a simple console application that will demonstrate the problem.
Also, you should note that Aspose.Words inits some static resources, that are kept in memory. So there might be small memory footprint after using Aspose.Words. But this allocated memory does not grow when you process more documents.