0

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?

kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
Palanikumar
  • 6,940
  • 4
  • 40
  • 51
  • 2
    Why do you want to do this? – poke Jul 06 '15 at 07:09
  • 1
    The recommendation is not doing it, Garbage Collector will do it for you. – Yohanes Nurcahyo Jul 06 '15 at 07:11
  • This is .NET - leave memory management to the Garbage Collector. There are best practices like calling Dispose() on IDisposable's but not sure what you trying to achieve here? – Kevin Jul 06 '15 at 07:11
  • @poke, the answer for your question is my question. After the form is closed the memory allocated for that form is not released. So I try to release the memory using that code. – Palanikumar Jul 06 '15 at 07:12
  • That doesn’t answer *why* you want to manually release the memory. Why do you care about it in the first place? – poke Jul 06 '15 at 07:13
  • What makes you think the memory is not being released? – Kevin Jul 06 '15 at 07:15
  • @YohanesNurcahyo and Kevin, The Garbage Collector / Dispose() is not releasing the memory after form closed. – Palanikumar Jul 06 '15 at 07:17
  • @poke and Kevin, When I looking my application process in Task Manager the memory is keep growing so sometime application throws Out Of Memory exception. – Palanikumar Jul 06 '15 at 07:20
  • "The Garbage Collector / Dispose() is not releasing the memory after form closed." Can you prove this statement using the Memory Profiler? – kyrylomyr Jul 06 '15 at 07:21
  • @PalaniKumar Then that’s a sign that you don’t dispose your objects properly. Calling the garbage collector manually won’t change that. – poke Jul 06 '15 at 07:22
  • @kirmir, I didn't used Memory Profiler before. I think you mention this http://memprofiler.com/. Let me check with this. – Palanikumar Jul 06 '15 at 07:24
  • Use any memory profiler (I used ANTS for example) and check what causes your forms to not release. – kyrylomyr Jul 06 '15 at 07:25
  • If there was a memory leak problem with WinForms people would have noticed 10 years ago. Your code isn't releasing memory when it should, or it's creating a lot of temporary objects (eg temporary strings) without releasing them. Forcing a garbage collection may *hide* the problem but it's still there – Panagiotis Kanavos Jul 06 '15 at 07:32

2 Answers2

1

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.

Community
  • 1
  • 1
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
1

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.

Community
  • 1
  • 1
kyrylomyr
  • 12,192
  • 8
  • 52
  • 79