It seems like Garbage Collector doesn’t clean up resources connected with ProgressBar control, and it causes the continuous growth of the number of User Objects of my WinForms application (seen in Task Manager).
Here is a sample code:
private void button1_Click(object sender, EventArgs e)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
panel1.Controls.Clear();
Random rnd = new Random((int)DateTime.Now.Ticks);
ProgressBar pB = new ProgressBar() { Value = rnd.Next(0, 100) };
panel1.Controls.Add(pB);
}
When the Button is clicked, the number of User Objects increases by 2-3. When the number of User Objects becomes 10000 an Exception is thrown. How can I prevent the growth of User Objects?
Please excuse any spelling or grammatical mistakes, English isn’t my
first language
You probably need to call
.Dispose()on your old progress bar before you clear the panel.