Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2007

Tip: Looking for answers? Try searching our database.

Multiple GC.Collect Calls...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ben.heebner@gmail.com - 24 Oct 2007 16:20 GMT
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.
Scott M. - 24 Oct 2007 16:50 GMT
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

> 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
[quoted text clipped - 13 lines]
>
> Thanks.
Peter Duniho - 24 Oct 2007 19:32 GMT
> [...]
> 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.  :)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.