I built a quick program that needed to loop through an enormous log file (a couple of million records) and find various bits and pieces from inside. Because the volume of da
to force GC.Collect to do collection, call it like this GC.Collect(2); this means you are asking the GC to do a full blocking GC for the whole heap, 2 means Generation 2 of your heap.
Do any of the objects used in your loop implement IDisposable?
The garbage collector is not guaranteed to run when you call Collect(). It simply flags the object for collection. The next time the GC runs it will "collect" the flagged object.
There is no way in .NET to force the GC to collect at a specific point in time - if you need this functionality you'll need to go to native code.
There is a way. I found this article, it is in spanish by sure you understand the code, it works, I tested [http://www.nerdcoder.com/c-net-forzar-liberacion-de-memoria-de-nuestras-aplicaciones/][1]
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
public static void alzheimer()
{
GC.Collect();
GC.WaitForPendingFinalizers();
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}