Azure Functions - nodejs and dot net core 2.2 memory consumption keeps growing until recycled

236 Views Asked by At

I have a simple http triggered azure function which retrieve scalar value from azure sql database and return it as json, the function is hosted using azure app service plan, i have tried both "dot net core 2.2 app package" and "nodejs" and both of them made the memory consumption keeps growing over time until the memory reach above 90% or 100% and then either the GC start working or the app gets recycled i don't know which of them happen but after that the memory back to normal and starts to grow again. the weird thing is that i have tried the same dot net core 2.2 code as csharp script and in that case it worked properly and the memory never grew more than it should.

Anyone has explanation for the difference between the dot net core app and CSX memory consumption?

In all cases i used x64 environment

And if you know how can i dump the memory for a specific azure function hosted in app service this would be helpful.

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace MyNamespace
{
    public static class GetNumber
    {
        [FunctionName("GetNumber")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log, ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            try
            {
                string code = req.Query["code"];
                if (code.Equals("123") == false)
                {
                    return new BadRequestObjectResult("Invalid API Call!");
                }
                if (int.TryParse(req.Query["id"], out int id) == false)
                {
                    return new BadRequestObjectResult("Invalid Id!");
                }

                var config = new ConfigurationBuilder()
                    .SetBasePath(context.FunctionAppDirectory)
                    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables()
                    .Build();
                var connString = config.GetSection("ConnectionStrings").GetSection("MyDBConnectionString").Value;
                int number = 0;
                using (var sqlConnection = new SqlConnection(connString))
                {
                    using (var sqlCommand = sqlConnection.CreateCommand())
                    {
                        sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
                        sqlCommand.CommandText = "GetNumber";
                        sqlCommand.Parameters.AddWithValue("Id", id);
                        sqlConnection.Open();
                        number = (int)await sqlCommand.ExecuteScalarAsync();
                        sqlCommand.Dispose();
                        sqlConnection.Close();
                    }
                }
                var result = new ContentResult();
                result.Content = "{\"number\":" + number + "}";
                result.ContentType = "application/json";
                result.StatusCode = StatusCodes.Status200OK;
                return result;
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message, ex);
                return (ActionResult)new OkObjectResult(new { number = 0 });
            }
        }
    }
}
1

There are 1 best solutions below

0
On

Manually collect memory dumps Using ProcDump.exe

For more information, follow the blog : https://blogs.msdn.microsoft.com/kaushal/2017/05/04/azure-app-service-manually-collect-memory-dumps/