As we all know this is how garbage collection should be forced
GC.Collect();
GC.WaitForPendingFinalizers();
Also C# allows us to check amount of memory currently in use.
GC.GetTotalMemory(false) // do not wait for GC
But when I call two collect and wait methods, then getting memory used I still see that memory is not cleared.
public class MemoryUsage
{
private byte[] mmr;
public MemoryUsage()
{
mmr = new byte[10000000]; // ~10 MB
}
~MemoryUsage()
{
Console.WriteLine("Finalizer");
}
}
public static void Main()
{
Console.WriteLine(String.Format("Initial memory usage: {0}", GC.GetTotalMemory(false)));
MemoryUsage m1 = new MemoryUsage();
MemoryUsage m2 = new MemoryUsage();
MemoryUsage m3 = new MemoryUsage();
Console.WriteLine(String.Format("I use memory: {0}", GC.GetTotalMemory(false)));
m1 = null;
m2 = null;
m3 = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine(String.Format("I did collect and waited, but memory still: {0}", GC.GetTotalMemory(false)));
GC.GetTotalMemory(true);
Console.WriteLine(String.Format("I only got amount of memory and: {0}", GC.GetTotalMemory(false)));
}
App is built in Release mode, result is:
Initial memory usage: 47568
I use memory: 30062576
Finalizer
Finalizer
Finalizer
I did collect and waited, but memory still: 30070520
I only got amount of memory and: 62184
What I found is that I can call just GC.GetTotalMemory(true) instead of WaitForPendingFinalizers and all finalizers are finished.
Is there any other way to make sure that GC finished? Thanks