473,789 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WaitForPendi ngFinalizers , 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 4057
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.co m

"Mrinal Kamboj" <mr***********@ oracle.com> wrote in message
news:%A******** ******@news.ora cle.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.WaitForPendi ngFinalizers
, 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.WaitForPendi ngFinalizers 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.WaitForPendi ngFinalizers , 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.ora cle.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.WaitForPendi ngFinalizers
, 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
waitforpendingf inalizers 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.ora cle.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
undeterminist ic .

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.WaitForPendi ngFinalizers
, 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.oracl e.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
waitforpendingf inalizers 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/WaitForFinalize rs
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
10645
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 using a BULK COLLECT clause with a SELECT statement and a TABLE() operator in a join. I am finding that this select statement either returns the wrong result or the right result. The wrong result is always the same... too many rows where the...
0
1507
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) <> "" And iCtr <= MAX_ALLOW_DBASE dbase.Execute "INSERT INTO DailySalesReport " & _ "SELECT table1.storeqs, table1.styleqs, table1.dateqs, table1.idnumberqs, table1.cashqs, table1.salesclerkqs, table1.qs,
16
6736
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 any similar method. I want to make sure GC keeps up with the loop. My reasoning if Thread.Sleep(1000) is called; GC will take priority it do its work, right? GC.Collect(); GC.WaitForPendingFinalizers(); System.Threading.Thread.Sleep(1000);
9
8566
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 a basic test. I read a bunch of data into a dataset by using a command and a data adapter object, .Dispose(ing) as I go. The moment the data is in the Dataset, the Mem Usage column in the Task Manager goes up by 50 MB (which is about right). I...
48
5596
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? I need to know because I'm looking for memory leaks in an application. It would be really helpful to be able to determine if an object after manually invoking the GC.Collect is only kept alive because it still
4
1980
by: svgeorge | last post by:
I NEED TO COLLECT FROM THE GRIDVIEW(DATASELECTED) IN TO A TABLE(SelectedPayment) -------------------------------------------------------------------------------- How TO COLLECT THE ROWS CHECKED IN CHECK BOX IN THE DATASELECTED TO ANOTHER GRID VIEW ON CLICLING BUTTON I NEED TO COLLECT FROM THE GRIDVIEW(DATASELECTED) IN TO A TABLE(SelectedPayment) SIMILLAR TO HOTMAIL MODEL.....CHECK THE MAILS AND BRING THE CHECKED DATA TO ANOTHER PAGE
9
3116
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 stays there. The memory hardly goes down. When I disconnect the client and the connect it again the memory could reclaim to 400MB. But it could go down to 50 MB.
3
3643
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 found.' CREATE OR REPLACE PROCEDURE PROCESS_ANGKASA(REF_NO varchar2)is v_cntr_code varchar2(16); v_receipt_code varchar2(3); start_time number; end_time number;
2
2436
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 about windows memory management - just because memory is freed, does not mean that the OS instantly removes it from your process. You need to be looking at the performance counters for this - to find out the actuall amount of memory your using....
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.