Home All Groups Group Topic Archive Search About

Multiple GC.Collect Calls...

Author
24 Oct 2007 3:20 PM
ben.heebner
Howdy everyone. I've been looking online for the past few week about
calls to the garbage collector in .NET.  Current I'm using code that
through inheirtance is using a class that invokes the GC.Collect call
every 10 minutes.  I'm working on a dataserver and the genius that put
in the GC.Collect code didn't know what they were doing.  They knew
they had a memeory leak and decided that it was probably the
inefficiency of the .NET Grabage Collector and not thei crappy code
(simply amazing).  Anyways, I was wondering what the implications of
calling the GC.Collect method would be if you didn't then make the
GC.WiatForPendingFinalizers call?  Would it just queue up all the
calls and when the garbage collector does run it'll run once for every
time you asked it to run?

I'd like some good ammunition for removing this code (besides the fact
it isn't doing what the developer intended).

Thanks.

Author
24 Oct 2007 3:50 PM
Scott M.
Well, first of all calling GC.Collect can actually cause your application to
perform worse than by not calling it.  Using GC.Collect is only needed in
the most memory intensive applications out there.  Most users will never
need it, nor should they ever use it.

Why?

Here are 2 huge "whys"....

1.  Normally, the GC operates in its' own thread, by calling it directly
from your code, you bring the operation of the GC into the current thread
that is running the rest of your code!  So, doing this can cause you
application to run slower than before you called it.

2.  An analogy for how the GC cleans up memory is disk defragmentation.  As
I'm sure you know, it's nice to have your disk defragmented from time to
time, but while the defragmentation is taking place, your pc performs very
slowly because of the process that is taking place behind the scenes.  This
is true with the GC as well.  It takes CPU cycles for the GC to run, calling
it every 10 minutes causes the pc to perform CPU intensive tasks more often
than is probably needed, thus making everything else that is happening every
10 minutes suffer the consequences.

The person who felt that the GC is "inefficient" just doesn't understand
what the GC is or how it operates.  They also don't understand that if they
are experiencing a memory issue, it's most likely not the GC's fault, it is
the fault of the poorly written code.

In short, the GC knows what its' job is and it does it quite well.  The
minute you start tinkering with the GC, you open a can of worms that can
make things much worse if you don't know what you are doing.  In most cases,
there is no need to do this.

As for not calling GC.WaitForPendingFinalizers, in multi-threaded
applications, not calling this can cause issues between objects running in
different threads that are queued up for heap removal.  More at:
http://msdn2.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx.

-Scott



<ben.heeb***@gmail.com> wrote in message
Show quote
news:1193239202.551963.237940@z24g2000prh.googlegroups.com...
> Howdy everyone. I've been looking online for the past few week about
> calls to the garbage collector in .NET.  Current I'm using code that
> through inheirtance is using a class that invokes the GC.Collect call
> every 10 minutes.  I'm working on a dataserver and the genius that put
> in the GC.Collect code didn't know what they were doing.  They knew
> they had a memeory leak and decided that it was probably the
> inefficiency of the .NET Grabage Collector and not thei crappy code
> (simply amazing).  Anyways, I was wondering what the implications of
> calling the GC.Collect method would be if you didn't then make the
> GC.WiatForPendingFinalizers call?  Would it just queue up all the
> calls and when the garbage collector does run it'll run once for every
> time you asked it to run?
>
> I'd like some good ammunition for removing this code (besides the fact
> it isn't doing what the developer intended).
>
> Thanks.
>
Author
24 Oct 2007 6:32 PM
Peter Duniho
ben.heeb***@gmail.com wrote:
> [...]
> I'd like some good ammunition for removing this code (besides the fact
> it isn't doing what the developer intended).

There should be no need to call GC.Collect() explicitly.  That should be
reason enough to remove that code.  :)

AddThis Social Bookmark Button