Reputation: 2407
I written a program in C# (WinForms) that has many forms. I show forms in this way:
Form_Sell frm = new Form_Sell();
frm.Show();
When I show many forms, used memory of my program increased and when I close all of new forms it doesn't decrease! (I check Used memory of my program in task manager) why it happens? and how can i do?
I used GC.Collect() in FormClosed Event of form. but it doesn't work(no effects on used memory)
I test it with empty forms. no controls and nothing. but when I show form memory increases and when I close it memory doesn't decrease!
Upvotes: 0
Views: 1214
Reputation: 316
I think you need to use:
frm.Dispose();
by this, it will release used memory. Then run GC again and see what happens.
Upvotes: 0
Reputation: 4166
The Garbage Collector is non-deterministic. It will only free up memory when it feels pressure to do so. As such, just because you closed a Form
doesn't mean it will immediately free the memory from it.
Therefore, just checking the Task Manager to see if the memory has been freed up for a single Form
is not a good way to detect a memory leak.
To really, truly force the GC to get rid of the memory, a single GC.Collect()
call isn't sufficient if there are items that hold native resources. Try this instead:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Try that, and see if your memory changes.
Upvotes: 1