Getting System.OutOfMemoryException just opening and closing Doc

I have a process that opens a word doc, does some font spacing clean up, adds a TOC field, updates all fields and saves. I am getting a System.OutOfMemoryException when I save the doc. The doc is somewhat large (8-9 MB, ~1400 pages). I have enabled Memory Optimization in the save settings. I scaled the processing back to just open the doc and save it and I still see the exception. The failure is happening on a server that has 18GB of memory. When I monitor memory usage, peek usage never goes above 50%. When I run this standalone on my development laptop (32GB memory) the process works. We are running with the latest version 22.1

This is the code I am running.

License license = new License();
license.SetLicense(asposeLicense);
Document sourceDoc = new Document(targetWord);
var theSaveOptions = Aspose.Words.Saving.SaveOptions.CreateSaveOptions(targetWord);
theSaveOptions.MemoryOptimization = true;
theSaveOptions.SaveFormat = SaveFormat.Docx;
logMessage(interactive, context, EventLevel.Verbose, DetailStatus.Success, eventAction, "Before Save After Open");
sourceDoc.Save(targetWord, theSaveOptions);
logMessage(interactive, context, EventLevel.Verbose, DetailStatus.Success, eventAction, "After Save After Open");

@gkalte Could you please attach the problematic document here for testing? We will check the issue and provide you more information.
You can also try using SaveOptions.TempFolder on your server to avoid OutOfMemoryException on your server.

large-orig.docx (9.0 MB)

Using the temp folder appears to resolve the problem.
Thanks

@gkalte It is perfect that SaveOptions.TempFolder helped to resolve the problem. I also tested your document on my side and unfortunately have not managed to reproduce the problem. I have tried several scenarios and all of them works fine on my side:

  • Open and save as DOCX.
  • Open and save as PDF.
  • Open, replace placeholder with TOC and save as DOCX and PDF.