We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

OutOfMemoryException when merging more than 100 documents

Hi Team,

we are using Aspose.Word.dll 10.3.0.0 license for generating word document.

we are merging more than 100 documents of size 600kb each and we are getting “OutOfMemoryException” error(please find the attachment for error ) when running the below code.

public void Merge(Document templateDocument, FileInfo[] filesToMerge, string Outputpath)
{
try
{
templateDocument.RemoveAllChildren();
// Loop thru each of the Word documents
foreach (FileInfo file in filesToMerge)
{
Document srcDoc = new Document(file.FullName);
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

DocumentBuilder builder = new DocumentBuilder(srcDoc);
templateDocument.AppendDocument(srcDoc,ImportFormatMode.UseDestinationStyles);
}
}
catch (Exception ex)
{
// Program.LogException(ex);
}
}


please see into this issue and proveid the solution for same.

Thnaks,
Samanvay

Hi Samanvay,

Thanks for your inquiry. Please also share your code snippet mechanism to make it more simpler.
Follow up the provided link for more details.

<a href="https://forum.aspose.com/t/63985</a><div><br></div>

Hi Imran,

Thnaks for reply, actually we are generating a single document by merging multiple document at that time we are getting this error. below is the code which we are using.

private void button2_Click(object sender, EventArgs e)
{

string FolderPath = @“E:\Projects\DocList”;
DirectoryInfo Redir = new DirectoryInfo(@"" + FolderPath);
FileInfo[] AllDotm = Redir.GetFiles("*.docx");
string templatefilename = @“E:\Projects\DocumentTemplate\BlankWordDocument.docx”;
string Outputfilename = @“E:\Projects\DocumentTemplate\MergeDoc.docx”;
Document templateDocument = new Document(templatefilename);

templateDocument.RemoveAllChildren();
foreach (FileInfo file in AllDotm)
{
Document srcDoc = new Document(file.FullName);
templateDocument.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);
}
templateDocument.UpdateFields();
templateDocument.Save(Outputfilename);

MessageBox.Show(“Document generated Successfully”);

}


Thanks,
Samanvay

Hi Samanvay,

Please use the latest version of Aspose.Words for .NET 10.8.0 and let us know, If you still face exception.

As you suggested , we are used Aspose.wrod 10.8.0 version.

we are not getting "out of memory exception". but when we merge 100 documents. Memory usage and CPU usage increase till 90%. it effect other process and system get slow down.

Please provide solution for memory and CPU usage

Hi
Samanvay,


Thanks for your inquiry. It is quite difficult to answer such questions because performance and memory usage all depend on complexity and size of the documents you are generating. But any ways I will try to give some recommendations here:

1) Please note that during processing, the document needs to be held wholly in memory. Therefore, memory and CPU usage are dependent on document size and document complexity. it is better to use few small documents instead of one huge document.

2) In your code, you are merging your documents into one large document and then using update fields method. Since upon updating fields, Aspose.Words rebuilds document layout, this operation can be quite CPU and memory consuming if your document is large. So I would suggest you to call update fields method for each particular document and then afterwards merge them into one large document. I cannot guaranty this will make process much faster, but it will be good to try.

3) Again, memory usage fully depends on document complexity, size and format of input file. Usually, Aspose.Words needs 10 times more memory than the original document size to build a DOM in the memory.

If we can help you with anything else, please feel free to ask.

Best Regards,

Thanks for reply,

If document taking memory we are fine with this.

We run same code with Aspose.Word.dll 10.3.0.0 license to and Aspose.Word.dll 10.8.0.0 license copy.

When we use Aspose.Word.dll 10.3.0.0 license to merge document but we don't not see CPU usage 90% .

We believe Aspose.Word.dll 10.8.0.0 have CPU Usage problem.

Hi Samanvay,


Thanks for your inquiry. I have modified your code a little, please try using the following code snippet and let us know your findings:

string FolderPath = @“E:\Projects\DocList”;
DirectoryInfo Redir = new
DirectoryInfo(@""
  • FolderPath);
FileInfo[] AllDotm = Redir.GetFiles("*.docx");

string templatefilename = @“E:\Projects\DocumentTemplate\BlankWordDocument.docx”;
string Outputfilename = @“E:\Projects\DocumentTemplate\MergeDoc.docx”;

Document templateDocument = new Document(templatefilename);
templateDocument.RemoveAllChildren();

foreach (FileInfo
file in AllDotm)
{
Document srcDoc = new Document(file.FullName);
srcDoc.UpdateFields();
templateDocument.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);
}

templateDocument.UpdateFields();

templateDocument.Save(Outputfilename);

Best Regards,