I searched a lot about releasing memory when form close, but I didn't find any solution for releasing memory used by form. Most of the answer in stackoverflow or other forums is form.Dispose() or GC.Collect() will not help for releasing memory.
But I found an article Release memory in Windows Form application using C# (http://codesmithdotnet.blogspot.com/2008/02/release-memory-in-windows-form.html)
Fortunately the code from the article working fine :)
public class MemoryManagement
{
[DllImportAttribute("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 FlushMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
}
My question is, Is anyone familiar about the method "SetProcessWorkingSetSize"? / How it works? / Is this way is good for releasing memory? Is this will cause any issue or this will make application slow?