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.
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.