I would like to ask for clarification on how .net Garbage Collector works in this case.
I have a static function in which I allocate the byte array over and over again. I am inserting byte array reference into the list. The reference to the byte array or the list is not passed outside this function.
public static void StressMem(TimeSpan duration)
{
var bgnTm = DateTime.Now;
var data = new List<byte[]>();
while (true)
{
var arr = new byte[1024];
data.Add(arr);
var difTm = DateTime.Now - bgnTm;
if (difTm > duration) break;
}
}
I was expecting the memory to be freed (after some time) when this function was finished. But this is not happening to me. Why is this happening?
dotnet 5.0.302