Avatar of kvnsdr
kvnsdr

asked on 

GC Collect Called Too Many Times?

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()?
C#

Avatar of undefined
Last Comment
gregoryyoung
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kvnsdr
kvnsdr

ASKER

I have a memory hog windows app that starts with memory approx. 54,000k @ 15 threads then approx 10 hours later it has grown to 227,000k @33 threads.

Unreal, I've looked carefully for any 'NEW' object creation is followed by a Finally{???.Dispose();}.....

This too is not enough....

Q. Any suggestions?
SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo