OutOfMemory when saving Word document with big images

Hi,

I get an System.OutOfMemoryException when saving a Word document.

I do something like that:

[…]

_documentBuilder = new DocumentBuilder(_document);
foreach(Image myImage in Images)
{
    _documentBuilder.InsertImage(myImage);
    _documentBuilder.Writeln();
}

[…]

_document.Save(fileName, AWord.SaveFormat.Doc);

I have 200 images (it might be more) that have a maximum width and height of 540 pixels each. If I resize my images to 120 x 120, it works fine, even with more images.

Most of the times, it crashes in the Save method:

mscorlib.dll!System.IO.MemoryStream.Capacity.set(int value = 45939584)mscorlib.dll!System.IO.MemoryStream.EnsureCapacity(int value) mscorlib.dll!System.IO.MemoryStream.Write(byte[] buffer = {byte[424239]}, int offset = 0, int count = 424239)mscorlib.dll!System.IO.BinaryWriter.Write(byte[] buffer)Aspose.Words.dll!ᮆ.Ễ.DoWrite(System.IO.BinaryWriter writer = {System.IO.BinaryWriter})Aspose.Words.dll!ᮆ.ᴯ.ි(System.IO.BinaryWriter ී = {System.IO.BinaryWriter})Aspose.Words.dll!ᮆ.ᵯ.DoWrite(System.IO.BinaryWriter writer = {System.IO.BinaryWriter})Aspose.Words.dll!ᮆ.ᴯ.ි(System.IO.BinaryWriter ී = {System.IO.BinaryWriter})Aspose.Words.dll!ᮆ.ὲ.ᾁ(Aspose.Words.Drawing.ShapeBase Ⴏ = {Aspose.Words.Drawing.Shape}, System.IO.BinaryWriter ී = {System.IO.BinaryWriter})Aspose.Words.dll!ᮆ.ὲ.ᾀ(Aspose.Words.Drawing.ShapeBase Ⴏ = {Aspose.Words.Drawing.Shape}, System.IO.BinaryWriter ී = {System.IO.BinaryWriter})Aspose.Words.dll!⥣.⦰.⦾(Aspose.Words.Drawing.ShapeBase Ⴏ = {Aspose.Words.Drawing.Shape})Aspose.Words.dll!⥣.⦰.⦺(Aspose.Words.Drawing.Shape Ⴏ = {Aspose.Words.Drawing.Shape})Aspose.Words.dll!⥣.⦰.ᾀ(Aspose.Words.Drawing.ShapeBase Ⴏ = {Aspose.Words.Drawing.Shape})Aspose.Words.dll!⥣.⦰.᾿(Aspose.Words.Drawing.ShapeBase Ⴏ = {Aspose.Words.Drawing.Shape})Aspose.Words.dll!⥣.⦰.⦶(Aspose.Words.Drawing.Shape Ⴏ = {Aspose.Words.Drawing.Shape})Aspose.Words.dll!⥣.⥢.VisitShapeStart(Aspose.Words.Drawing.Shape shape = {Aspose.Words.Drawing.Shape})Aspose.Words.dll!Aspose.Words.Drawing.Shape.Accept(Aspose.Words.DocumentVisitor visitor = {⥣.⥢})Aspose.Words.dll!Aspose.Words.CompositeNode.࠿(Aspose.Words.DocumentVisitor ࡀ = {⥣.⥢})Aspose.Words.dll!Aspose.Words.Paragraph.Accept(Aspose.Words.DocumentVisitor visitor = {⥣.⥢})Aspose.Words.dll!Aspose.Words.CompositeNode.࠿(Aspose.Words.DocumentVisitor ࡀ = {⥣.⥢})Aspose.Words.dll!Aspose.Words.Body.Accept(Aspose.Words.DocumentVisitor visitor = {⥣.⥢})Aspose.Words.dll!⥣.⥢.ᒔ(Aspose.Words.Section ᇞ = {Aspose.Words.Section})Aspose.Words.dll!⥣.⥢.⥺()Aspose.Words.dll!⥣.⥢.ᒍ(՞.ᖄ ᒎ = {՞.ᖄ})Aspose.Words.dll!Aspose.Words.Document.ࠤ(System.IO.Stream ف = {System.IO.FileStream}, string ظ = "C:\Exports\Word\bah.doc", Aspose.Words.SaveFormat ࠥ = Doc)Aspose.Words.dll!Aspose.Words.Document.Save(string fileName = "C:\Exports\Word\bah.doc", Aspose.Words.SaveFormat fileFormat = Doc) 

Sometimes, the crash can be when Insert images in the document builder:

Aspose.Words.dll!ص.ش.ٙ(System.IO.Stream ٗ = {System.IO.MemoryStream})Aspose.Words.dll!Aspose.Words.DocumentBuilder.InsertImage(System.IO.Stream stream = {System.IO.MemoryStream}, Aspose.Words.Drawing.RelativeHorizontalPosition horzPos = Column, double left = 0.0, Aspose.Words.Drawing.RelativeVerticalPosition vertPos = Paragraph, double top = 0.0, double width = -1.0, double height = -1.0, Aspose.Words.Drawing.WrapType wrapType = Inline)Aspose.Words.dll!Aspose.Words.DocumentBuilder.InsertImage(System.Drawing.Image image = {System.Drawing.Bitmap}, Aspose.Words.Drawing.RelativeHorizontalPosition horzPos = Column, double left = 0.0, Aspose.Words.Drawing.RelativeVerticalPosition vertPos = Paragraph, double top = 0.0, double width = -1.0, double height = -1.0, Aspose.Words.Drawing.WrapType wrapType = Inline)Aspose.Words.dll!Aspose.Words.DocumentBuilder.InsertImage(System.Drawing.Image image = {System.Drawing.Bitmap}, double width = -1.0, double height = -1.0)
Aspose.Words.dll!Aspose.Words.DocumentBuilder.InsertImage(System.Drawing.Image image = {
    System.Drawing.Bitmap
})

Do you have suggestions in order to avoid these exceptions?

Thank you,
Marie

Hi

Thanks for your request. The problem might occur because you generate an extremely large document. To build document object model in memory, Aspose.Words requires few times more memory than the original size of the document. So the only workaround I can suggest you is avoiding generating such large documents.
Also, it would be great if you create a simple application which will allow me to reproduce the problem on my side.
Best regards.

Hi Alexey,

Looking at Aspose’s examples, I realized I could dispose my images right after the InsertImage call. I am now making sure that I do that all the time and the memory doesn’t get as high as it used to. There is still a big peak when saving the document, but it is tolerable for now.

I did a sample project. I can send it to you if you want. It basically does the following:

try
{
    Document document = new Document();
    DocumentBuilder builder = new DocumentBuilder(document);
    for (int i = 0; i < nbrImages; i++)
    {
        Console.WriteLine("Insert image number " + i + "...");
        // We always reuse the same images, that we downscale...
        using (Bitmap bitmapImage = CopyAndResizeImage(images[i % 4], 540, 540))
        {
            // Modify the image to pretend we used different images
            int x = i % bitmapImage.Width;
            int y = i % bitmapImage.Height;
            Color color = bitmapImage.GetPixel(x, y);
            int luma = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
            bitmapImage.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
            // Send to Aspose
            builder.Write("Image number " + i + ". ");
            builder.InsertImage(bitmapImage);
            builder.Writeln();
        }
    }
    document.Save(path, SaveFormat.Doc);
    MessageBox.Show(this, "Export done successfully.", "Export successful");
}
catch (Exception ex)
{
    MessageBox.Show(this, ex.ToString(), "Export failed.");
}

I can send you the whole project if it can help. In my case, I crash 100% of the time with 1500 images. The crash occurs when saving the document.

Thank you,
Marie

Hi Marie,

Thank you for additional information. It is not very good practice to generate such huge documents. What is the expected file size of document with 1500 images inside? Even MS Word does not like such large documents, sometimes it just hangs upon opening large documents.
So the only way to get rid such problems, I can suggest you at the moment, is avoiding generating huge documents.
Best regards.

Maybe you could integrated the java db H2 to buffer large documents…

Hi
Thanks for your suggestion. I think, you can try specifying temporary folder upon saving huge documents:
https://reference.aspose.com/words/java/com.aspose.words/docsaveoptions/#getTempFolder
This can help to avoid out of memory exceptions.
Best regards,

Turns out we had to increase PermGen size and enable parallel gc…

Thanks for you help anyway :slight_smile: