By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,693 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,693 IT Pros & Developers. It's quick & easy.

Forcing the garbage collector to run

P: n/a
I know I read everywhere that you "can't" force the
garbage collector to run, and that you really have no
control when it runs.

However as a programmer I know that there is always a way
around something.

So I ask you, how do you force the garbage collector to
run and clean up?
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
GC.Collect will manually force it to fire..whether or not you actually want
to do that is another question.

You can also pass in a Param for the generation so you can just collect
specific generations.

HTH,

Bill
"IcedCrow" <ch********@aol.com> wrote in message
news:36****************************@phx.gbl...
I know I read everywhere that you "can't" force the
garbage collector to run, and that you really have no
control when it runs.

However as a programmer I know that there is always a way
around something.

So I ask you, how do you force the garbage collector to
run and clean up?

Nov 20 '05 #2

P: n/a
IcedCrow,
I know I read everywhere that you "can't" force the
garbage collector to run, and that you really have no
control when it runs. I've never read that you can't force the GC to run, only that you should
normally not force it to run.

As William stated, you can use GC.Collect to force the garbage collector to
run.

Generally you should follow GC.Collect with a call to
GC.WaitForPendingFinalizers, then you should follow this with another call
to GC.Collect, followed by a second call to GC.WaitForPendingFinalizers.

As GC.Collect by itself will put objects into the finalization list,
GC.WaitForPendingFinalizers will remove these object from the list.
GC.Collect will then clean up these finalizable objects. Which may have
created more finalizable objects...

However it is highly advisable not to call GC.Collect as it takes care of
itself and you will potentially hurt performance rather then help
performance. The GC is self tuning, and if you call GC.Collect, the GC may
get 'out of sync'.

The following two articles covers when you should & should not call
GC.Collect.

http://msdn.microsoft.com/msdnmag/issues/1100/gci/
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/

Hope this helps
Jay

"IcedCrow" <ch********@aol.com> wrote in message
news:36****************************@phx.gbl... I know I read everywhere that you "can't" force the
garbage collector to run, and that you really have no
control when it runs.

However as a programmer I know that there is always a way
around something.

So I ask you, how do you force the garbage collector to
run and clean up?

Nov 20 '05 #3

P: n/a
Hi IcedCrow,

Just to add to Jay's references. (The article is excellent, by the way)

In part 2 of the article there's a link to Figure 1 which uses
JavaScript. It failed on my machine. If it fails for you too, the full
link
is http://msdn.microsoft.com/msdnmag/is...I2/figures.asp.

Regards,
Fergus
Nov 20 '05 #4

P: n/a
Hello,

"IcedCrow" <ch********@aol.com> schrieb:
I know I read everywhere that you "can't" force the
garbage collector to run, and that you really have no
control when it runs.

However as a programmer I know that there is always a way
around something.


In addition to the other links you should read the following chapter of the
documentation:

http://msdn.microsoft.com/library/en...collection.asp

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.