I am using aspose Words for .NET to generate large reports from database data. The reports can be saved in three formats html, docx, and doc.
When calling Document.Save using Html format the report is saved correctly without issue. When using Html format I can successfully produce documents containing 20,000 records which result in a 182MB html file.
However when saving the document in either docx or doc format I run in to the following 'Out of Memory' Exceptions as detailed below. Even running 5000+ records in these formats causes these issues. (Expected docx size 3MB, expected doc size 30MB)
The issue is that checking the PC's Available memory performance counter I see that I have plenty (almost 1.3GB) of free memory when this exceptions occurs. Using a Process 'virtual memory analysis' utility I see that the process also has 1GB of free virtual available memory. However due to memory fragmentation the biggest contiguous block is approx 250MB.
Therefore it appears (from examinig the exceptions below) that the problem is due to the fact that aspose is trying to create a very large memorystream which is bigger than the largest contiguous memory block available in the processes virtual memory space, resulting in an 'Out of Memory' exception.
Although we were aware that there was an 'Out of Memory' issue before chosing Aspose, we beleived that it was due to the fact that we had really run out of memory. We had planned to work around this issue by monitoring memory usage and splitting the files in to multiple documents when memory became an issue. However as explained above it seems we cannot simply predict when this 'Out of Memory' exception will occur as it seems to be dependant on the present largest contiguous memory block in the processes virtual memory space.
Therefore answers to any of the following would help us to determine the best way forward with this issue:
Is Apsose.word trying to stream the whole document in one huge memorystream before writing to file in docx/doc format? (Hopefully not) Or does Aspose.words just stream one section/chunk before writing it to file?
Is aspose aware of the above issue, or have any plans to resolve this issue by writing is chunks?
Why can Aspose successfully save such large Html Document without running in to this issues?
Is there any way I can add sections/settings to the document such that this problem will not occur?
Is there a recommended max document size or any better method of predicting when this 'Out of Memory' problem will occur?
Thanks in advance
Daniel Finkelstein
Senior Developer - HP Software
Docx Exception:
Message:
Exception of type 'System.OutOfMemoryException' was thrown.
Stack Trace:
at System.IO.MemoryStream.set_Capacity(Int32 value)
at System.IO.MemoryStream.EnsureCapacity(Int32 value)
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(Char value)
at System.Xml.XmlTextWriter.WriteEndAttributeQuote()
at System.Xml.XmlTextWriter.AutoComplete(Token token)
at System.Xml.XmlTextWriter.WriteEndAttribute()
at System.Xml.XmlWriter.WriteAttributeString(String localName, String value)
at ؓ.⎛.⎪(String ހ, String Ӷ)
at ⎃.⎂.⇿(String ⎌, String Ӷ)
at ㍺.§×.VisitParagraphStart(Paragraph para)
at Aspose.Words.Paragraph.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ս(DocumentVisitor Վ)
at Aspose.Words.Tables.Cell.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ս(DocumentVisitor Վ)
at Aspose.Words.Tables.Row.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ս(DocumentVisitor Վ)
at Aspose.Words.Tables.Table.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ս(DocumentVisitor Վ)at Aspose.Words.Body.Accept(DocumentVisitor visitor)
at
㍺.㎓.㎬(CompositeNode ㎭)...
at
㍺.㎍.DoWrite()...
at Aspose.Words.Document.ԅ(Stream Ӿ, String Ӽ, SaveFormat Ԇ)
at Aspose.Words.Document.Save(String fileName, SaveFormat fileFormat)
Doc Exception:
Message:
Exception of type 'System.OutOfMemoryException' was thrown.
Stack Trace:
at System.IO.MemoryStream.set_Capacity(Int32 value)at System.IO.MemoryStream.EnsureCapacity(Int32 value)
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
...
at Aspose.Words.Document.ԅ(Stream Ӿ, String Ӽ, SaveFormat Ԇ)
at Aspose.Words.Document.Save(String fileName, SaveFormat fileFormat)