473,386 Members | 1,754 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,386 software developers and data experts.

GC.Collect

Hi ,

Any pointers when it's absolute necessary to use it .

Does it has a blocking effect on the code , as GC per se is
undeterministic .

what if GC.collect is followed in next line by
GC.WaitForPendingFinalizers , will it actually block .

Why i need all this info as in my stress test suite developer is
stressing upon the need to use these things in every iteration of every
thread , where simultaneously around 200 worker thread are working , so
i am not very convinced about such a high frequency usage .

TIA ,

Mrinal
Nov 17 '05 #1
5 4034
Mrinal,

What is your test suite developer's reason for calling GC.Collect?

If you called GC.Collect on 200 worker threads, it's going to do you
more harm or goo.

Basically, don't do it, it's not a good idea. The GC.Collect method is
there because it has to be (because there are VERY rare cases where it needs
to be used), but in general, you should never be calling it.

Having 200 worker threads, it would seem that calling GC.Collect would
be more of a hinderance.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mrinal Kamboj" <mr***********@oracle.com> wrote in message
news:%A**************@news.oracle.com...
Hi ,

Any pointers when it's absolute necessary to use it .

Does it has a blocking effect on the code , as GC per se is
undeterministic .

what if GC.collect is followed in next line by GC.WaitForPendingFinalizers
, will it actually block .

Why i need all this info as in my stress test suite developer is stressing
upon the need to use these things in every iteration of every thread ,
where simultaneously around 200 worker thread are working , so i am not
very convinced about such a high frequency usage .

TIA ,

Mrinal

Nov 17 '05 #2
Mrinal,

Yes, GC.WaitForPendingFinalizers blocks until all finalizers (which are
on the finalization queue) have run to completion. That's straight out
of the documentation.

So the test suite developer wants you to call GC.Collect on every
iteration of every thread? You're not the only one that's not
convinced. Is the developer claiming that performance is enhanced by
doing this? He is a test developer so as crazy as this sounds I have
to assume, at least for now, that he has actually tested his
hypothesis. Is that the case?

I'm also concerned about the number of threads. 200 is a lot of
threads. Thread pooling is almost certainly a more appropriate
solution.

Brian

Mrinal Kamboj wrote:
Hi ,

Any pointers when it's absolute necessary to use it .

Does it has a blocking effect on the code , as GC per se is
undeterministic .

what if GC.collect is followed in next line by
GC.WaitForPendingFinalizers , will it actually block .

Why i need all this info as in my stress test suite developer is
stressing upon the need to use these things in every iteration of every
thread , where simultaneously around 200 worker thread are working , so
i am not very convinced about such a high frequency usage .

TIA ,

Mrinal


Nov 17 '05 #3
"Mrinal Kamboj" <mr***********@oracle.com> wrote in message
news:%A**************@news.oracle.com...
Hi ,

Any pointers when it's absolute necessary to use it .
I'll try give you a few.. But first a few rules

Rule 1) Never assume you know how many generations .NET uses or their sizes.
(Sure we have 3 of them and gen0 is 256K, but this may change in future
systems and differ on platforms)

Rule 2) The GC knows best when to collect. (It keeps stats for gen2 and
collect at will, while gen1 and gen0 gets collected when full. This may
change in future systems.)

However. There are cases when you may want to collect. These could be:

1) When you are in a huge complex (i.e nested) loops and know you are
allocating plenty of reference-types that is fire and forget (read strings).
Hence they won't survive the outer loop but exist in the inner. I'm talking
about if you got O(N^3) and know that the code will allocate several
thousands or millions of objects. Well for each 65'000 of those objects the
gen0 in .NET 1.1 will get full, the GC kicks in and move most of them to
gen1. And when gen1 gets full the'll be moved to gen2. This is not what you
want for your short lived objects and you can help the GC to collect. By
collecting often you leave more space for other objects.

2) When using COM. If you automate Excel or Word, the marshalling generates
a lot of objects that tends to stick and move to gen1 and gen2 really fast.
If you write a lot of data to excel it is just fair to do a collect when
you're done with the filling.

3) When .NET is eating memory like a hog and you don't know why. A
GC.Collect() is a great debugging-tool for finding poor performance. Just
move that line around in the code until .NET stops eating memory like
Godzilla chews up Tokyo and you just found a place to refactor. =)

Does it has a blocking effect on the code , as GC per se is
undeterministic .
Yes. When the GC kicks in it will block just about everything,one
application domain at a time. But the GC lives in his own thread and you
will hardly notice a gen0 collect. But if gen2 have grown to giga-size and
start collect and re-arrange you will feel a stagger and winamp may lag. =)

This is why you might wanna help the GC sometimes.
what if GC.collect is followed in next line by GC.WaitForPendingFinalizers
, will it actually block .
Yes. or else it would imply that the GC was buggy.
Why i need all this info as in my stress test suite developer is stressing
upon the need to use these things in every iteration of every thread ,
where simultaneously around 200 worker thread are working , so i am not
very convinced about such a high frequency usage .


Well, 200 threads is quite a lot for one application. And as you are
stressing another app, you are stressing your dear .NET runtime with
context-switching. Hence this would be an excellent case for when you want
to help the GC to collect.

But then again, for most applications there is no need to collect. Rule 1
and 2 applies.

Happy Collecting
- Michael S

Nov 17 '05 #4
Hi All ,

Thanks for the suggestions / pointers , they will definitely help me in
making a wise decision .

just to divulge some more details , i am the developer of the stress
test suite , which is stressing another app. and checking it's
performance under load , where for around 3 hrs. , all 200 threads keep
on going in loop , with a sleep time of 1sec. after every iteration ,
but what i make sure is :

1. No major allocations are done in that loop , essntially not a very
heavy work per iteration .

2. Before leaving the loop , relevant objects are closed , disposed and
nullified , so that's why i never thought it to be necessary to go in
collect method .

but on other end App. developer doesn't want to take a blame of some
arrant mem. leak / handle leak , so he's bent upon using collect and
waitforpendingfinalizers in all iterations , however i don't think by
doing this i am simulating a real scenario .

any more suggestions , most welcome .

thanks ,

Mrinal

Michael S wrote:
"Mrinal Kamboj" <mr***********@oracle.com> wrote in message
news:%A**************@news.oracle.com...
Hi ,

Any pointers when it's absolute necessary to use it .

I'll try give you a few.. But first a few rules

Rule 1) Never assume you know how many generations .NET uses or their sizes.
(Sure we have 3 of them and gen0 is 256K, but this may change in future
systems and differ on platforms)

Rule 2) The GC knows best when to collect. (It keeps stats for gen2 and
collect at will, while gen1 and gen0 gets collected when full. This may
change in future systems.)

However. There are cases when you may want to collect. These could be:

1) When you are in a huge complex (i.e nested) loops and know you are
allocating plenty of reference-types that is fire and forget (read strings).
Hence they won't survive the outer loop but exist in the inner. I'm talking
about if you got O(N^3) and know that the code will allocate several
thousands or millions of objects. Well for each 65'000 of those objects the
gen0 in .NET 1.1 will get full, the GC kicks in and move most of them to
gen1. And when gen1 gets full the'll be moved to gen2. This is not what you
want for your short lived objects and you can help the GC to collect. By
collecting often you leave more space for other objects.

2) When using COM. If you automate Excel or Word, the marshalling generates
a lot of objects that tends to stick and move to gen1 and gen2 really fast.
If you write a lot of data to excel it is just fair to do a collect when
you're done with the filling.

3) When .NET is eating memory like a hog and you don't know why. A
GC.Collect() is a great debugging-tool for finding poor performance. Just
move that line around in the code until .NET stops eating memory like
Godzilla chews up Tokyo and you just found a place to refactor. =)
Does it has a blocking effect on the code , as GC per se is
undeterministic .

Yes. When the GC kicks in it will block just about everything,one
application domain at a time. But the GC lives in his own thread and you
will hardly notice a gen0 collect. But if gen2 have grown to giga-size and
start collect and re-arrange you will feel a stagger and winamp may lag. =)

This is why you might wanna help the GC sometimes.

what if GC.collect is followed in next line by GC.WaitForPendingFinalizers
, will it actually block .

Yes. or else it would imply that the GC was buggy.

Why i need all this info as in my stress test suite developer is stressing
upon the need to use these things in every iteration of every thread ,
where simultaneously around 200 worker thread are working , so i am not
very convinced about such a high frequency usage .

Well, 200 threads is quite a lot for one application. And as you are
stressing another app, you are stressing your dear .NET runtime with
context-switching. Hence this would be an excellent case for when you want
to help the GC to collect.

But then again, for most applications there is no need to collect. Rule 1
and 2 applies.

Happy Collecting
- Michael S

Nov 17 '05 #5

"Mrinal Kamboj" <mr***********@oracle.com> wrote in message
news:xt************@news.oracle.com...
Hi All ,
but on other end App. developer doesn't want to take a blame of some
arrant mem. leak / handle leak , so he's bent upon using collect and
waitforpendingfinalizers in all iterations , however i don't think by
doing this i am simulating a real scenario .

Mrinal,

Sounds to me that you are absolutely right. A key goal of a stress test is
to expose memory and resource leaks. By calling Collect/WaitForFinalizers
you are hiding problems due to your developer not correctly calling Dispose
when required. While the finalizer will eventually clean up the resource, in
a realistic scenario that won't happen till the GC gets around to it - in
it's own time. By pre-empting that decision you are hiding potential
problems. (I am assuming here that this is all happening in the same
AppDomain - i.e. what you are testing is some sort of library.)

Of course your developer may have other reasons - so be tactful in tellling
him he's wrong :-)

--------
Nigel Norris
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul Rowe | last post by:
Hi "You" I have two collection types declared at the SQL level. 1. Do you know of any known bugs with the BULK COLLECT clause used with the TABLE operator? I have a situation now where I am...
0
by: nick_faye | last post by:
Hi, I hope somebody can help me. I am collecting data from different external ms access database on my VB programming. I am using the SQL command as shown below: While strPaths(iCtr) <> ""...
16
by: LP | last post by:
Hi, Considering code below. Will it make GC to actually collect. One application creates new instances of a class from 3rd party assembly in a loop (it has to). That class doesn't have .Dispose or...
9
by: Frank Rizzo | last post by:
I understand the basic premise: when the object is out of scope or has been set to null (given that there are no funky finalizers), executing GC.Collect will clean up your resources. So I have...
48
by: Ward Bekker | last post by:
Hi, I'm wondering if the GC.Collect method really collects all objects possible objects? Or is this still a "smart" process sometimes keeping objects alive even if they can be garbage collected?...
4
by: svgeorge | last post by:
I NEED TO COLLECT FROM THE GRIDVIEW(DATASELECTED) IN TO A TABLE(SelectedPayment) -------------------------------------------------------------------------------- How TO COLLECT THE ROWS...
9
by: =?Utf-8?B?TWlrZTk5MDA=?= | last post by:
Hello, I am wondering if it is a good idea to use GC.Collect() in a timer. For example, timer is fired every 5 minutes and calls GC.Collect(). In our server app the memory goes to 600MB and...
3
by: oravm | last post by:
Hi, I re-write a query and used bulk collect to improve the performance of the batch process. The query below has NO compile error but when execute query there is error 'ORA-01403: no data...
2
by: Tom Shelton | last post by:
On 2008-04-15, DR <softwareengineer98037@yahoo.comwrote: Where are you seeing that? In the task manager? If so, then you are looking in the wrong place. Let me tell you a little something...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.