Search code examples
azureout-of-memorymemorystreamaspose.words

System.OutOfMemoryException in Azure but not locally


I have the following code which uses a third party library called Aspose.Words:

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);
    return ms.ToArray();
}

I hit the following error on the using statement.

Mvc.ExceptionHandling.AbpExceptionFilter - Exception of type 'System.OutOfMemoryException' was thrown.

I'm unable to reproduce it locally (my memory usage goes up by perhaps 200mb while its processing so it doesn't really use much memory). The file itself is only 56MB in size. I'm told by Aspose it could use up to 10 times that amount... even still it should be fine. I've altered the Service plan to scale up to 14GB of memory.

I'm using RecyclableMemoryStreamManager because I already tried it with MemoryStream to no avail.

What else could I do to problem solve this issue that I'm only hitting in Azure (dev & production)? Azure API is .net core stack and has a platform of 64bit.

Thanks in advance.


Solution

  • What Azure Service offering are you using?

    Most App Service based offerings, including Azure Functions in consumption plan maxxes out at 1.5GB memory per process/app regardless of the app service pricing plan you choose. Azure functions in consumption plan have a max memory limit of 1.5GB per function app instance (scaling can allow multiple instances) and other offerings also have limits to prevent you from gobbling up all the memory of the underlying machines. (Your local environment does not have these limits)

    It is hard to solve without actual metrics. Run your code, then give it a few minutes after the crash and see metrics for the app, especially under Working Memory and private bytes. These are found in the metrics section in the App Service.

    If these hit around the limits before your crash, this is likely the reason. If this is the case, your options in Azure are

    • Reduce your memory footprint
      • Optimize the current work so it uses less memory
      • Or split the work into multiple jobs which each uses less memory and use Azure Functions or similar
    • use an App Service Environment (lot of memory, isolated networking, but not cheap)
    • use a VM (Azure got memory optimized VMs for practically any size)