I created a simple code just to test something is happening with my app on docker. Im just creating 1gb of bytes to test the memory usage on my app dockerized, and what i realized is that the memory is always going up. The first request went from 20 MB to 800 MB then to 1GB , then to 1.3gb and so on. Im so curious how memory works on .Net apps works cause the memory never goes down, like you can se in the image. Even when my api returns the data and we dont need it anymore, at leats for the request. But what i care about more is that the memory usage is going up and up but it doesnt go down to the start point. So if it reachs the limit of kubernets it just fails. This is my example code:
public static IEnumerable<HeavyWeatherForecast> GenerateHeavyForecast(int count)
{
return Enumerable.Range(1, count).Select(index => new HeavyWeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)],
LargeData = new byte[1024 * 1024]
});
}
Of course we can optimize the code, but i just wanted to test the memory usage and behavior in this scenarios. Help me please , im just to newbie to make a better diagnostic, i want to improve.
I tried to call de GC manually and expect the memory to went down, but stills behaving the same. Set this parameters ServerGarbageCollection, ConcurrentGarbageCollection to true.