1

I have this Azure function which is loading a file into memory:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;

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

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            var strings = new List<string>(1000000);

            using (var fs = Task.FromResult(File.OpenRead(@"C:\secretfile.txt")))
            using (var reader = new StreamReader(fs.Result))
            {
                while (!reader.EndOfStream)
                {
                    var rowText = reader.ReadLine();

                    strings.Add(rowText);
                    strings.Add(rowText + "C");
                    strings.Add(rowText + "W");
                }

                return new OkObjectResult(responseMessage);
            }
        }
    }
}

After the function has finished, how can I get it to release the memory? The memory is not freed.

diagnostic tools memory

I'm trying to understand this because I have an Azure function in an App Service plan in Azure that does something similar to this. I am concerned it will just hang on to the memory, so it won't be available to other Apps in the same App Service plan.

CraftyFox
  • 165
  • 2
  • 11
  • It might be so, because the GC doesn't release memory to the OS immediately. Execute the function multiple times and see if the memory usage increases constantly. – Christian.K Apr 05 '23 at 04:08
  • Yes, it releases the memory on subsequent calls because it needs it. But I don't won't it holding onto the memory when it's not getting called. – CraftyFox Apr 05 '23 at 20:00
  • It is generally not a good idea to mess with the GC ;-), letting it manage the memory is usually better (it will also sense that the system is getting low on memory and free some more/aggressively). You could check [GC config settings](https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector) (search for "retain"), if you really want to. – Christian.K Apr 06 '23 at 12:54

1 Answers1

0

This depends on the version of .NET Core you are using. You will need to learn the CLR Profiling API for whichever version of .NET Core you are using.

.NET Core 2.2 - use this profiler:

ICorProfilerInfo10 Interface

https://github.com/dotnet/docs/blob/e5fccd84b508544367433ce6a825946cbdfc0123/docs/framework/unmanaged-api/profiling/icorprofilerinfo10-interface.md

.NET Core 3.0 - use this profiler:

ICorProfilerInfo9 Interface

https://github.com/dotnet/docs/blob/e5fccd84b508544367433ce6a825946cbdfc0123/docs/framework/unmanaged-api/profiling/icorprofilerinfo9-interface.md


You will need to compile these DLL files (based on each method) or download the DLL. And call the DLL using the API documentation for each method. This requires you are using C++. I'm not sure if there is a client version to profile .NET Core, like they have for .NET framework.

Years ago, this was released for .NET Framework:

https://en.wikipedia.org/wiki/CLR_Profiler


You should be able to download Visual Studio Code (for free) and install the C++ extension. Then install C++ on your machine. This will be a different installation depending on your operating system. Once you do that, download the appropriate DLL, and write a short C++ application that calls the available methods in the documentation above. I'm not sure how to profile your .NET Core code with this C++ application. You could probably message one of the owners of this interface on GitHub on how to do that. Sorry I can't help more than this.

At the very end of this video, it explains where to look with the log of the actual issues. "Get a snapshot in rider. And analyze the snapshot from the profiling tool window. Where we can start our performance investigation and look at metas and cultries." Skip to 1:00 exactly.

https://www.youtube.com/watch?v=ZWS156lKAos


If you have a C# project built, you can add this configuration to find out if Server GC is causing the memory leak:

<PropertyGroup> 
    <ServerGarbageCollection>false</ServerGarbageCollection>
</PropertyGroup>
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245