The #1 Rule for .NET Performance
Tags: architecture, dotnet, performance
Published: 2018-03-16 15:23:02

---
title: The #1 Rule for .NET Performance
published: true
tags: Architecture,dotnet,performance
canonical_url: http://www.elemarjr.com/en/2018/03/the-1-rule-for-net-performance/
---

 ![](http://www.elemarjr.com/wp-content/uploads/2018/03/chuttersnap-381972-unsplash-300x200.jpg)

If you ask me one tip to improve the performance of your applications, it would be:

**Design your objects to be collected on `gen #0`  or not at all.**

Naturally, following this recommendation demands you to [know, at least the basics, of how the garbage collection works](http://If%20you%20ask%20me%20one%20tip%20to%20improve%20the%20performance%20of%20your%20applications,%20it%20would%20be:%20Design%20your%20objects%20to%20be%20collected%20on%20gen%20#0%20or%20not%20at%20all.%20Naturally,%20following%20this%20recommendation%20demands%20you%20to%20know%20how%20the%20garbage%20collection%20works.%20But,%20if%20you%20are%20interested%20in%20improving%20the%20performance%20of%20your%20applications,%20this%20is%20necessary%20at%20all.%20In%20fact,%20the%20garbage%20collector%20was%20explicitly%20designed%20to%20be%20very%20efficient%20performing%20gen%20#0.%20Garbage%20collector%20gets%20more%20expensive%20in%20each%20generation.%20Even%20with%20background%20processing%20for%20gen%20#2,%20there%20is%20still%20a%20very%20high%20CPU%20cost%20to%20pay.%20Consider%20that%20you%20should%20avoid%20gen%20#1%20collections%20too.%20In%20most%20scenarios,%20gen%20#1%20objects%20are%20directly%20promoted%20to%20gen%20#2.%20Ideally,%20every%20object%20you%20allocate%20goes%20out%20of%20scope%20by%20the%20time%20next%20gen%20#0%20comes%20around.%20You%20can%20measure%20how%20long%20is%20that%20interval%20and%20compare%20it%20to%20the%20duration%20that%20data%20is%20alive%20in%20your%20application.%20Time%20to%20action%20Following%20this%20recommendation%20requires%20a%20shift%20in%20your%20mindset.%20It%20will%20inform%20nearly%20every%20aspect%20of%20your%20application.). But, if you are interested in improving the performance of your applications, this is necessary at all.

In fact, the garbage collector was explicitly designed to be very efficient performing gen #0.

Garbage collector gets more expensive in each generation. Even with background processing for gen #2, there is still a very high CPU cost to pay. Consider that you should avoid gen #1 collections too. In most scenarios, gen #1 objects are directly promoted to gen #2.

Ideally, every object you allocate goes out of scope by the time next gen #0 comes around. You can measure how long is that interval and compare it to the duration that data is alive in your application using tools such as [PerfView](https://github.com/Microsoft/perfview) (By the way, take some time and learn how to use PerfView – this is such great tool).

## Time to action

Following this recommendation requires a shift in your mindset. It will inform nearly every aspect of your application (I strongly recommend you this [Ayende’s talk about this topic](https://www.youtube.com/watch?v=eRjWYjEf8n4&feature=youtu.be)).

Here are some guidelines to keep in mind when writing your code:

- Pool Long-lived and large objects – Consider [following the same patterns adopted by the Roslyn team](http://www.elemarjr.com/en/2017/06/roslyn-performance-lessons-1-the-objectpool-and-the-pooledstringbuilder/).
- Avoid copying data whenever you can. Use classes as [ArraySegment](https://msdn.microsoft.com/pt-br/library/1hsbd92d(v=vs.110).aspx) and, now, the new Span\<T\> (Please, take some time and [read this MSDN Magazine](https://msdn.microsoft.com/en-us/magazine/mt814808.aspx) written by Stephen Toub).
- Reduce References between Objects – That would make the GC work less expensive. Also, it makes easier to predict object lifetimes.
- Be aware of [the cost of finalizers](https://ayende.com/blog/181665-A/the-cost-of-finalizers).
- Use Weak References for Caching

Isn’t that enough? In the next weeks, I will share in-depth information about how you could follow these recommendations to get improved .NET applications performance. Sign in my contacts list, and you will receive notification when new content arrives.

Last but not least, remember that [Performance is a feature](https://blog.codinghorror.com/performance-is-a-feature/). If the performance of your application is not that good, chances are you missing opportunities to deliver business value.

Cover image: [chuttersnap](https://unsplash.com/@chuttersnap?utm_medium=referral&utm_campaign=photographer-credit&utm_content=creditBadge "Download free do whatever you want high-resolution photos from chuttersnap")

The post [The #1 Rule for .NET Performance](http://www.elemarjr.com/en/2018/03/the-1-rule-for-net-performance/) first appeared on [ElemarJR.com](http://www.elemarjr.com/en).