Aspose Word : System.OutOfMemoryException: Insufficient memory to continue the execution of the program

Hi Aspose,

We have a exception from Aspose Word on a production server (WINServer 2016 64 bits 8gb of memory).
The memory grow from 800kb to 7 GB and throw this exception.
The final document size in PDF is 109kb, the docx model size is 97 kb.

We don’t arrive to reproduce this exception on the dev server which is a coy of your production server.

System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
at System.Text.StringBuilder.ExpandByABlock(Int32 minBlockCharCount)
at System.Text.StringBuilder.Append(Char value, Int32 repeatCount)
at System.Text.StringBuilder.Append(Char value)
at ? . (String )
at .? ?(String , Boolean , Boolean , IFieldResultFormatter )
at Aspose.Words.Fields.FieldFormat.(? , String& )
at Aspose.Words.Fields.FieldFormat.(? , String& )
at ?? .()
at ?? .
?? ?()
at ?? .(Field )
at ?? . (?? )
at ?? .(?? )
at ?? .(?? )
at ?? .(?? , ?? )
at ?? .(Field , ? )
at ?? .(Field , ? )
at ?? .(Field , ? )
at
.(Field , , Boolean )
at ? .( ? , ?? )
at ? .( ? , IDictionary`2 , ?? )
at ? .( ? )
at .(Document , ? )
at ? .? ?()
at ? .(?? )
at ?? .(?? , ?? )
at . ()
at .(? , ? [] , Boolean )
at Aspose.Words.MailMerging.MailMerge.(? )

Have you a idea of of ​​what can create this exception ?
Thanks for your help

@Syl20

Please use SaveOptions.MemoryOptimization property as shown below to perform memory optimization. Hope this helps you.

Document doc = new Document(MyDir + "SaveOptions.MemoryOptimization.doc");
// When set to true it will improve document memory footprint but will add extra time to processing. 
// This optimization is only applied during save operation.
SaveOptions saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Pdf);
saveOptions.MemoryOptimization = true;

doc.Save(MyDir + "SaveOptions.MemoryOptimization.pdf", saveOptions);

Hi Tahir,

We have already active this MemoryOptimisation option

Aspose.Words.Saving.SaveOptions option = new Aspose.Words.Saving.PdfSaveOptions(); option.MemoryOptimization = true;
option.SaveFormat = SaveFormat.Pdf;
report.Save(stream, option);

I can try to replace the creation of PdfSaveOption instance with the call of CreateSaveOptions method…

@Syl20

Could you please attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.

A post was split to a new topic: Aspose Word Library try to connect to Internet

Hi Tahir,

We have finaly found the reason of this exception.

If you integrate a new Quik Part ->Field -> NumPages in the footer of the first page of the model, with a dev pc or server with Internet Access, you have no problems to generate the document in pdf. If the server in behind a WAF without internet access, Aspose Word memory usage grow and crach with the exception in the first Post.

Regards,

@Syl20

You can use LoadOptions.ResourceLoadingCallback property to control how external resources (images, style sheets) are loaded when a document is imported.

You can use following code example to skip such resources while importing document.

public class HandleResourceLoading : IResourceLoadingCallback
{
    public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
    {
        String url = args.OriginalUri;
        if (args.ResourceType == ResourceType.Image || url.Contains("http://"))
            return ResourceLoadingAction.Skip;

        return ResourceLoadingAction.Default;
    }
}

LoadOptions loadOptions = new LoadOptions();
loadOptions.ResourceLoadingCallback = new HandleResourceLoading ();
var document = new Document(MyDir + "input.docx", loadOptions);

If you still face problem, please ZIP and attach your input document here for testing. We will investigate the issue and provide you more information on it.