System.OutOfMemoryException when saving larger documents

I am injecting some text into a footer and then save the file. However I seem to have issues when the file size is larger. A 56mb file while saving will give me this error: System.OutOfMemoryException

This is not an issue for me when I develop locally. Its only an issue when I’m using Azure no matter what resources I give Azure.

I’ve seen in other posts mentions of memory optimization and recyclableMemoryStream but unfortunately these give the same error.

This is my current code which works for smaller files and not bigger ones (which I’ve classed as above or below 25mb)

if (isBigFile)
{
    SaveOptions saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Docx);
    saveOptions.MemoryOptimization = true;
    var mm = new RecyclableMemoryStreamManager();
    //does not work
    using (RecyclableMemoryStream ms = new RecyclableMemoryStream(mm))
    {
        doc.Save(ms, saveOptions);
        return ms.ToArray();
    }
}
else
{
    //works
    using (MemoryStream dataStream = new MemoryStream())
    {                 
        doc.Save(dataStream, GetSaveFormatType(fileType));
        dataStream.Position = 0;
        return dataStream.ToArray();
    }
}

@RCAtWork,

Please upgrade to the latest version of Aspose.Words for .NET i.e. 19.12 and see how it goes on your end? Hope, this helps.

In case the problem still remains, please ZIP the word document you are getting this problem with, upload the .zip file to Dropbox or any other file hosting service and provide the download link here for testing. Please also provide a simplified console application (source code without compilation errors) that helps us to reproduce the problem on our end. We will then investigate the issue on our end and provide you with more information. Thanks for your cooperation.

Hi, I’ve upgraded to 19.12 but there is no improvement.
Here is the file: Dropbox - large file with bmp inserted.zip - Simplify your life

The code is:

Aspose.Words.Document doc = new Aspose.Words.Document(stream);
stream.Close();
InsertFooterWatermark(doc, text);
SaveOptions saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Docx);
saveOptions.MemoryOptimization = true;
saveOptions.TempFolder = Path.GetTempPath();
var mm = new RecyclableMemoryStreamManager();
using (RecyclableMemoryStream ms = new RecyclableMemoryStream(mm))
{
    doc.Save(ms, saveOptions);
    ms.Position = 0;
    return ms.ToArray();
}
var bytes = memoryStream.ToArray();
memoryStream.Close();
return bytes;

private void InsertFooterWatermark(Aspose.Words.Document doc, string text)
    {           

        foreach (Section section in doc)
        {
            if (section.HeadersFooters.Count == 0)
            {
                DocumentBuilder builder = new DocumentBuilder(doc);
                builder.MoveToSection(doc.Sections.IndexOf(section));
                builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
                builder.Font.Name = "Tahoma";
                builder.Font.Size = 7;
                builder.Font.Bold = true;
                builder.Write($"[{text}] ");
                builder.Font.Bold = false;
            }
            else
            {
                foreach (HeaderFooter footer in section.HeadersFooters)
                {
                    if (footer.HeaderFooterType == HeaderFooterType.FooterPrimary ||
                        footer.HeaderFooterType == HeaderFooterType.FooterFirst ||
                        footer.HeaderFooterType == HeaderFooterType.FooterEven)
                    {
                        DocumentBuilder builder = new DocumentBuilder(doc);
                        builder.MoveToSection(doc.Sections.IndexOf(section));
                        builder.MoveToHeaderFooter(footer.HeaderFooterType);
                        builder.Font.Name = "Tahoma";
                        builder.Font.Bold = true;
                        builder.Font.Size = 7;
                        builder.Write($"[{text}] ");
                    }
                }
            }
        }
    }

The error is below which occurs on the line starting with “using (Recycl…”

ERROR 2019-12-17 17:37:14,470 [4 ] Mvc.ExceptionHandling.AbpExceptionFilter - Exception of type ‘System.OutOfMemoryException’ 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  .(Stream , Stream )
at ​ .(Stream , Boolean , Int32& )
at ​ .(​ ,  )
at ​ .(Stream )
at  .(OleFormat )
at  .(Shape )
at  .(Shape )
at  .VisitShapeEnd(Shape )
at Aspose.Words.Drawing.Shape.r3ka8zscesabkstb24kjjv9sml9wq7pw (DocumentVisitor )
at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)
at Aspose.Words.Drawing.Shape.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)
at Aspose.Words.Paragraph.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)
at Aspose.Words.Body.Accept(DocumentVisitor visitor)
at  .(CompositeNode )
at  .()
at  . ()
at  .()
at  .()
at  . ( )
at  . ( )
at Aspose.Words.Document.( , SaveOptions )
at Aspose.Words.Document.(Stream , String , SaveOptions )
at Aspose.Words.Document.Save(Stream stream, SaveOptions saveOptions)
at Bowie.Framework.Portal.FileProcessing.WordProcessor.AddWatermark(Stream stream, String fileType, String text, String statusName, String userName, String timezoneId) in d:\a\1\s\Portal\aspnet-core\Bowie.Framework.Portal.FileProcessing\WordProcessor.cs:line 56
at Bowie.Framework.Portal.Document.DocumentStorageService.AddWatermark(Doc document, FileContent fileContent, Int64 userId) in d:\a\1\s\Portal\aspnet-core\src\Bowie.Framework.Portal.Application\Document\DocumentStorageService.cs:line 157
at Bowie.Framework.Portal.Document.DocumentStorageService.GetDocumentDownloadUrl(Doc document, Int64 userId) in d:\a\1\s\Portal\aspnet-core\src\Bowie.Framework.Portal.Application\Document\DocumentStorageService.cs:line 201
at Bowie.Framework.Portal.Document.DocsAppService.DownloadDocument(Int32 docId) in d:\a\1\s\Portal\aspnet-core\src\Bowie.Framework.Portal.Application\Document\DocsAppService.cs:line 642
at lambda_method(Closure , Object )
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()

@RCAtWork,

We are checking this scenario on our end and will get back to you soon with our findings.

@RCAtWork
Thank you for additional information. I tested your scenario with Azure Functions and managed to reproduce it in my side. Also I tested your code and document in a simple .NET Core console application and it eats about 1GB of memory but successfully process the file. So I agree with the answer in StackOverflow, that you reached the memory limit.
Such monstrous documents are very rare case. Also I suspect the document you have shared is not a real document, but was created synthetically. I do not think we can do something here to decrease memory usage. I can only suggest you not to use such huge documents and split them into smaller document.

Hi Alexey, in the end I had to limit the documents to 50mb. This was not ideal. These docs do have a lot of pictures in them.

@RCAtWork It is great that you managed to resolve the problem on your end. Unfortunately there is little we can do on our side to reduce memory usage upon processing such documents.