C# memory leak after calling another form

William James 6 Reputation points
2022-07-13T09:13:07.837+00:00

Is This Problem Called Memory Leak? My Application include 5 Different Form. I Showing One of Them by Using this Code :

Form2 frm2 = new Form2();
frm2.ShowDlialog();
Form2 Includes : Buttons, TextBox, Lables, Listbox to Add Items Into the ListBox. (ADO.NET)

When frm2 Appears, It Get 1MB Memory (Based on Task Manager). After Closing Form2, The Memory Still High And it Does not Decrease. If I Call frm2.ShowDlialog() For Multiple Time, Application Memory Increased Every Time.

What I have tried:

I Searched in Google and tried :

Number 1 :

Form2 frm2 = new Form2();
frm2.ShowDlialog();
frm2.Dispose();

Number 2 : (in Form2_FormClosing)

GC.Collect();

Note: For example, When I Run Internet Download Manager software and open Dialog Form , The Memory Usage not Increased. What should I do to avoid Memory Leaks in My WinForms?

Developer technologies | C#
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Zahurul 1 Reputation point
    2022-07-13T09:49:29.26+00:00

    try using block to ensure the release of resources properly.
    using (var frm2 = new Form2())
    {
    frm2.ShowDialog();
    }


  2. Ken Tucker 5,861 Reputation points
    2022-07-13T09:50:42.297+00:00

    Hard to say without seeing the code for Form2. I would make sure you are removing any event handlers you add in code when the form is closing


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.