473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Forcing the garbage collector to run

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
4 13850
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Nick Jacobson | last post by:
In the Python documentation on Extending and Embedding (in section 1.10), there's a quote: "Maybe some day a sufficiently portable automatic garbage collector will be available for C. Until...
3
by: Martin Drautzburg | last post by:
Just for curiosity: does python use a mark-and-sweep garbage collector or simple reference counting? In the latter case it would not garbage collect circular references, right ? For...
10
by: pachanga | last post by:
The Hans-Boehm garbage collector can be successfully used with C and C++, but not yet a standard for C++.. Is there talks about Garbage Collector to become in the C++ standard?
1
by: MAHESH MANDHARE | last post by:
hi To all , I want to know about garbage collector & freeing memory of objects i have writened one sample program and in that i created one destructor something like this using System;...
4
by: PromisedOyster | last post by:
There are various contradictory newsgroup postings on this issue, but I would really like a definitive answer from the .NET gurus out there? We have various WinForms that contain multiple Icons...
13
by: Mingnan G. | last post by:
Hello everyone. I have written a garbage collector for standard C++ application. It has following main features. 1) Deterministic Finalization Providing deterministic finalization, the system...
72
by: Sebastian Nibisz | last post by:
SGCL is precise, parallel garbage collection library for C++ (at this time for Windows 32/64 only). SGCL is free software published under University of Illinois/NCSA Open Source License. Get it...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
46
by: Carlo Milanesi | last post by:
Hello, traditionally, in C++, dynamically allocated memory has been managed explicitly by calling "delete" in the application code. Now, in addition to the standard library strings, containers,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.