I'm trying to figure out (and hopefully reduce) memory usage in my app. I've tracked down one large memory usage to when I allocate a byte of an image. That byte is transient, but doesn't seem to be garbage collected. I've managed to reproduce the issue in a simple repro:
Console.WriteLine("A " + GC.GetTotalMemory(true)); byte bytes = new byte[2048 * 2048 * 4]; Console.WriteLine("B " + GC.GetTotalMemory(true)); GC.Collect(); Console.WriteLine("C " + GC.GetTotalMemory(true)); bytes = null; System.Runtime.GCSettings.LargeObjectHeapCompactionMode = System.Runtime.GCLargeObjectHeapCompactionMode.CompactOnce; GC.Collect(); Console.WriteLine("D " + GC.GetTotalMemory(true));
When I run this early in my app it works as expected:
A 4196960 B 21001016 C 21001024 D 4223816
But when I run it later in the middle of my app the memory is not recovered:
A 5081416 B 21858520 C 21858520 D 21858520
It's running exactly the same code. Does anyone have any ideas?