Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make .NET Garbage Collection less frequent?

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.

like image 893
zzandy Avatar asked Feb 08 '11 09:02

zzandy


2 Answers

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:

  • are you doing string concatenation in a loop that could be StringBuilder instead?
  • any moderate-to-large collections that could be initialized with the correct length to avoid reallocations?
  • any scratch-buffers that could be reused? for example, do you do a lot of encoding work that uses a byte[] briefly in a method, where that byte[] could be passed in as a scratch area, or grabbed from a pool?
  • any other things that could be cached and re-used - for example, a new Regex("some literal") created inside a method, that could be made into a static field, plus compiled for performance
  • any intensive string parsing that could use interning? (not necessarily the inbuilt interner though)
like image 146
Marc Gravell Avatar answered Nov 13 '22 23:11

Marc Gravell


You 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>
like image 42
leppie Avatar answered Nov 13 '22 22:11

leppie