I have an application that procesess large number of small objects, e.g. 2000 messages per second. One message is roughly 100 bytes, maybe less. The application ran for 6 and a half hours under load and during that time it had 264 416 0th gen collections, 166 699 1st gen and 69 608 2nd gen. This is 11.6, 7.3 and 3 collections per second respectively.
The question is how to make garbage collection less frequent?
UPD: Application is a server receiving messages from WCF, pughing them though several processing modules and saving them to database.
I was under impression that GC should adapt and increase generation size after some time but this is obviousely not the case.
UPD 2: as suggested in leppie's answer GC in server mode indeed makes 10 times less collections. Also it appears (as Richter describes it) that frequent collections are not a bad thing.
What makes you think this is impacting you? In particular, gen-0 collections are very, very cheap - and to be encouraged.
I would look more at ""What might cause some of these requests to escape gen-0 into gen-1; am I clinging onto any object unnecessarily, for example via an event?".
Of course, another thing to look at is: are there any places you can avoid creating unnecessary objects, for example:
StringBuilder
instead?byte[]
briefly in a method, where that byte[]
could be passed in as a scratch area, or grabbed from a pool?new Regex("some literal")
created inside a method, that could be made into a static field, plus compiled for performanceYou will get about 10 times less GC's if you enable gcServer="true"
in the app.config. But do note, this does not improve performance, and you will have likely have increased latency in your application if it is a desktop application.
Here is app.config setting:
<runtime>
<gcServer enabled="true"/>
</runtime>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With