Azure Function OutOfMemoryException after 100 MB allocation

Oliver Seitz 21 Reputation points
2021-11-27T16:55:54.103+00:00

According to the documentation, the memory limit for a consumption plan azure function should be 1.5 GB. While running some tests because we received quite a few OutOfMemory exceptions we ran into the following behavior.

Expected: Execute the for loop about 15 times to allocate about 1.5 GB of memory, then fail with an OutOfMemory exception

Actual: Executes a single loop, then fails with an OutOfMemory exception

I am using an Azure Function v4, .NET 6.0, with the following code:

    public static class Function  
    {  
        private const int MByteSize = 1_048_576;  
  
        [FunctionName("MemoryTestAzureFunction")]  
        public static async Task Run(  
            [ServiceBusTrigger("dummy", Connection = "ServiceBusConnectionString")]  
            string myQueueItem, ILogger log)  
        {  
            log.LogInformation("> Started Run: {StartTime}", DateTime.UtcNow.ToLongTimeString());  
            LogMemory(log);  
  
            var buffer = new byte[20][];  
  
            for (var i = 0; i < 20; i++)  
            {  
                log.LogInformation(" > Allocating 100 MB");  
                buffer[i] = new byte[MByteSize * 100];  
                  
                LogMemory(log);  
  
                log.LogInformation(" > Filling 100 MB");  
                Array.Fill
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,908 questions
{count} votes

4 answers

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,506 Reputation points
    2022-01-03T06:15:33.983+00:00

    There is a known issue in the platform for x64 bit Function Apps running on Windows in dynamic sku. Please refer to this github issue : https://github.com/Azure/azure-functions-host/issues/7881

    Mitigation :
    Add the App Setting: "COMPLUS_GCHeapHardLimit" to the value 0x60000000

    The permanent fix for this issue will be released in the first quarter of this year 2022.

    2 people found this answer helpful.

  2. Alberto Spelta 1 Reputation point
    2021-11-29T14:01:12.867+00:00

    I faced the same problem, the OutOfMemoryException issue occurs when the functions app platform is set to 64bit.
    I changed the platform of my functions to 32bit and the problem seems to be solved.

    More info here where the same issue has been fixed for the runtime version v3
    https://github.com/Azure/azure-functions-host/issues/5430

    0 comments No comments

  3. Oliver Seitz 21 Reputation points
    2021-11-29T14:14:47.177+00:00

    Runtime is set to 64 bit, does not help.


  4. Robin Dijkstra 1 Reputation point
    2021-12-30T12:41:18.24+00:00

    I also experienced OutOfMemory exceptions, escpecially with ConvertTo-Json, although the data only contains about 4 MB.
    I used a new Azure Functions v4, 64-bit, on the consumption plan.
    After some experimenting, I discovered that when downgrading to v3 the errors disappeared.
    I couldn't upgrade to v4 once more to double-check but perhaps this helps in your case also.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.