Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

.NET Performance Tip – Know Your Garbage Collection Options

0.00/5 (No votes)
7 Oct 2018 3  
An under-utilised setting that can offer substantial performance gains

Introduction

An under-utilised setting that can offer substantial performance gains.

Workstation GC - is what you’ll be getting by default with a .NET application and might be unaware of another option. It uses smaller segments, which means more frequent collections, which in turn are short, thus minimising application thread suspensions. When used with concurrent GC, it is best suited for desktop / GUI applications. With concurrent disabled (all threads will suspend for GC), it uses a little less memory and is best suited for lightweight services on single-core machines, processing intermittently (appropriate use cases are few and far between). 

There Is Another Option!

Server GC- the one you should try - if you have multiple processors dedicated to just your application, this can really speed up GC, and often allocations too. GCs happen in parallel on dedicated threads (one for each processor/core), facilitated by a heap per processor. Segments are larger, favouring throughput and resulting in less frequent, but longer GCs. This does mean higher memory consumption.

I mentioned a concurrent GC setting above (since .NET4, this is called background GC). From .NET4.5, it is enabled by default in both Server and Workstation GC. I don’t expect you’ll ever change it but good to know what it brings to the table - with it enabled, the GC marks (finds unreachable objects) concurrently using a background thread. It does mean an additional thread for every logical processor in Server GC mode but they are lower priority and won’t compete with foreground threads.

Add this to your app.config for Server GC: <configuration> <runtime> <gcServer enabled="true"/> </runtime> </configuration> If you have good reason, you can disable background GC:

<gcConcurrent enabled="false"/>

Disclaimer: As with all performance work, measure impact before and after to confirm it’s the right choice for your application!

Update 7.10.2018

We can now configure a limit on the number of processors a process uses with Server GC on shared boxes, without needing containers (rather than just defaulting to Workstation GC).

See this link for details of how to configure this feature.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here