Palanikumar
Palanikumar

Reputation: 7160

Release memory when form close

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?

Upvotes: 0

Views: 7798

Answers (2)

Litisqe Kumar
Litisqe Kumar

Reputation: 2564

As already pointed by Hans Passant in another question

SetProcessWorkingSetSize() controls the amount of RAM that your process uses. SetProcessWorkingSetSize is typically used to increase the amount of RAM allocated for a process.

1:- How it works?

Ans:- Suppose I have many Forms or dialog boxes in my application, I want to call SetProcessWorkingSetSize() after I close each form or dialog box, so that the OS frees the resources.

2:- Is this way is good for releasing memory?

Ans:-I think No. Windows is already quite good at dynamically controlling this, swapping memory pages out on demand when another process needs RAM. By doing this manually, you slow down your program a lot, causing a lot of page faults when Windows is forced to swap the memory pages back in.

3:-Is this will cause any issue or this will make application slow?

Ans:-Yes mentioned in answer 2.

Upvotes: 1

kyrylomyr
kyrylomyr

Reputation: 12652

You should release only the external unmanaged resources (close opened connections, file handlers, etc.) and do that using the Dispose method implemented by that resources.

No need to try to force GC to release managed objects related to Forms. In 99% cases don't call GC at all.

No need to use external Windows API methods, it's not a .NET way. Read how memory management works on MSDN.

You should find why your managed objects are not disposed.

As for WinForms: most problems might be caused by left event handlers. Check more details on already answered question Memory Leaks in Winforms application. Use any Memory Profiler (e.g. ANTS) in order to find references which keeps your Forms objects from releasing.

Upvotes: 1

Related Questions