Click here to Skip to main content
15,902,299 members
Articles / Garbage collection
Tip/Trick

.NET Performance Tip – Know Your Garbage Collection Options

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
7 Oct 2018CPOL2 min read 22.5K   20   6
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Ben is the Principal Developer at a gov.uk and .NET Foundation foundation member. He previously worked for over 9 years as a school teacher, teaching programming and Computer Science. He enjoys making complex topics accessible and practical for busy developers.


Comments and Discussions

 
QuestionCan gcServer be set to true programmatically somehow? Pin
RenniePet3-Sep-18 2:29
RenniePet3-Sep-18 2:29 
AnswerRe: Can gcServer be set to true programmatically somehow? Pin
GerVenson8-Oct-18 3:14
professionalGerVenson8-Oct-18 3:14 
QuestionNeed more info Pin
Lazy Heap15-Jul-18 19:35
Lazy Heap15-Jul-18 19:35 
AnswerRe: Need more info Pin
Ben Hall (failingfast.io)16-Jul-18 11:05
professionalBen Hall (failingfast.io)16-Jul-18 11:05 
There's no difference. Just a name thing. See my post: https://stackoverflow.com/questions/48607982/how-do-concurrent-background-and-server-garbage-collection-differ-in-c
SuggestionMore Details Please PinPopular
Alois Kraus15-Jul-18 12:04
Alois Kraus15-Jul-18 12:04 
AnswerRe: More Details Please Pin
Ben Hall (failingfast.io)16-Jul-18 11:07
professionalBen Hall (failingfast.io)16-Jul-18 11:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.