Q. Can the GC.Collect() be called too many times?
I've seen examples where it's used one time withing the Main entry point like this:
[STAThread]
static void Main()
{
GC.Collect();
GC.WaitForPendingFinalizers();
Application.Run(new Form1());
}
Other examples call GC.Collect() every time a method or object finishes, like so:
private void timer1_Tic(object sender, System.EventArgs e)
{
try
{
// perform method
}
catch(Exception ex)
{
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
Q. What's the correct use of GC.Collect()?
Our community of experts have been thoroughly vetted for their expertise and industry experience.