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

Free Support Forum - aspose.com

System.OutOfMemoryException when creating DOC document with "large" pictures

Hi

I am using Aspose.Words for .Net on my web server to generate a Word (.doc) document, which is then sent to the client browser. The document contains a number of pictures with related text and I (and my users) are periodically experiencing System.OutOfMemoryExceptions. (See the stack trace below)

Mostly, the document will contain between 5 or 30 pictures, which are scaled with DocumentBuilder.InsertImage(String FileName, double width, double height) where width is set to 200 and height is set to 150. The original pictures that are inserted in the document are 1600 x 1200 pixels before inserted into the document.

As written above, the problem only occurs periodically, even with the exact same pictures and text. Therefore I can see that the size of the resulting Word document is only about 2 MB, which I think should be no problem to handle for the web server related to memory etc.

I would of course like to solve this problem, and would therefore like to know whether I can change anything in my code. I suspect that a probable explenation could be that the InsertImage method is maybe using a lot of memory on my large pictures, that isn’t released in proper time. If that is the case, I might be able to resize the image in other ways before using the InsertImage method.

Can anybody help me with a way to avoid this OutOfMemoryException?

Thanks

Nikolaj



System.Web.HttpUnhandledException: Exception of type ‘System.Web.HttpUnhandledException’ was thrown. —> System.OutOfMemoryException: Exception of type ‘System.OutOfMemoryException’ was thrown.
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.BinaryWriter.Write(Byte[] buffer)
at ᰆ.ẝ.DoWrite(BinaryWriter writer)
at ᰆ.ḳ.৭(BinaryWriter ৮)
at ᰆ.Ṳ.DoWrite(BinaryWriter writer)
at ᰆ.ḳ.৭(BinaryWriter ৮)
at ᰆ.ἶ.ὅ(ShapeBase ഞ, BinaryWriter ৮)
at ᰆ.ἶ.ὄ(ShapeBase ഞ, BinaryWriter ৮)
at ㋟.㌱.㌿(ShapeBase ഞ)
at ㋟.㌱.㌻(Shape ഞ)
at ㋟.㌱.ὄ(ShapeBase ഞ)
at ㋟.㌱.ώ(ShapeBase ഞ)
at ㋟.㌱.㌷(Shape ഞ)
at ㋟.㋞.VisitShapeStart(Shape shape)
at Aspose.Words.Drawing.Shape.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ս(DocumentVisitor Վ)
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 ㋟.㋞.⌱(Section )
at ㋟.㋞.㋵()
at ㋟.㋞.〈(⍝ 〉)
at Aspose.Words.Document.ԅ(Stream Ӿ, String Ӽ, SaveFormat Ԇ)
at Aspose.Words.Document.Save(String fileName, SaveFormat fileFormat, SaveType saveType, HttpResponse response)
at XXX.YYY.FD.sendFilAfsted()
at XXX.YYY.FD.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
— End of inner exception stack trace —
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.FD_aspx.ProcessRequest(HttpContext context) in c:\windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\FD\e50a14c1\8e0de10d\App_Web_s_yspilm.8.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. The problem might occur because size of the generated document is too large. Aspose.Words requires few times more memory to build Document Object Model than the original document size.

Maybe in your case, if you need to resize images, you can do that before inserting images into the document. This can help you to reduce size of the generated document. Here you can find code example, which shows how to resize images:

http://www.aspose.com/community/forums/130397/merged-image-size/showthread.aspx#130397

Hope this helps.

Best regards.

Hi

Thanks for the quick reply.

However, before spending 15-20 hours on modifying, testing and distributing my code I would like to be pretty certain that it will fix the problem I am currently experiencing :slight_smile:

So I would like to know if the following assumption is correct:

- With my currect code (using InsertImage with “large files”), Aspose.Words keeps an internal representation, including the data in the large files, until the “Save” method is called? That means, that if I add 25 files of 400 kb each, a lot of data will be kept in memory until I call the Save method, or experience an OutOfMemory exception?

- If I change the code, such that each image file is resized before being inserted with the “InsertImage” method, the memory consumption will be much lower? E.g. when resized, each image would probably be 30 kb or similar, before inserted into the Word document.

Can you confirm that this a correct assumption, or does the internal memory handling work in other ways?

Thanks

Nikolaj

Hi Nikolaj,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry.

1. Yes, all content will be kept in memory until you call Save method.

2. Yes, if you resize images before inserting them in the document, the images will take less memory in the model.

Best regards.