What Do These …<>+c… Classes Do in my Memory Snapshots?

There’s nothing we love as much as user feedback. It is a priceless source of insights into how people use tools like dotMemory, what gets them excited – and what gets them confused.

Over the last year we’ve received multiple questions from users seeing classes with ...+<>c... in their names. They said:

  • “…in the object browser we have some instances of an object that ends with +<>c. I can’t find any information what kind of objects this is and I hope that you can help me?”
  • “What does +<>c mean? I am new to dotMemory.”
  • “I am looking at some “Survived Objects” and I see a lot of “MyClass+<>c“.”
  • “dotMemory lists a lot of objects that look like “ClassName+<>c...” with no explanation.”

Let’s look at what these are!

In dotMemory, we may see the following:

How lambda looks like in a memory snapshot

So, what is the mysterious ...+<>c... class? The answer is quite simple. It’s a helper class the compiler creates to execute a lambda. Actually, we’ve already written about this here in a series of posts on how to fight performance issues in .NET. Let me try to sort out the details one more time, especially considering that there were some changes in the compiler since then.

In this example, we have some StringUtils class that has a Filter method used to apply filters on a list of strings:

We want to use this method to filter out strings that are longer than 3 symbols. We will pass this filter to the condition argument using a lambda. Note that the resulting code (after compilation) will depend on whether the lambda contains a closure (a context that is passed to a lambda, e.g. a local variable declared in a method that calls the lambda).

Let’s take a look at the possible ways to call the Filter method: via a lambda without closure, via a lambda with closure, and via a method group.

A lambda with no closure


First, let’s use a lambda without a closure. Here we pass the maximum string length (3) as a number, so no additional context is passed to the lambda:

Decompiled code

If we decompile this code in dotPeek (our free decompiler), we’ll get the following:

As you can see, the compiler has created a separate <>c class with a static constructor, making the lambda a method in this class (b__0_0). The method is called via the <>9__0_0 static field. Note that this field is checked against null and created only once during the first call.

Console.WriteLine(StringUtils.Filter(source, FilterTestNoClosure.<>c.<>9__0_0 ?? (FilterTestNoClosure.<>c.<>9__0_0 = new Func<string, bool>((object) FilterTestNoClosure.<>c.<>9, __methodptr(b__0_0)))).Count);

You may wonder what advantages this approach may have. The answer is that it won’t generate any memory traffic. Even if you call FilterLongString ten thousand times, the lambda will be created just once.

What about dotMemory?

To ensure, let’s see how the following code looks in dotMemory:

The Memory Traffic view in dotMemory will look as follows:

Memory traffic from a lambda without closure

As you can see, only one object FilterTestNoClosure+<>c is created. If we examine this instance using the Key Retention Paths view, we’ll see that it is retained via its static fields. Note that as any static members, these fields will remain in memory for the entire lifetime of the application. But typically, this is not a big deal as such static objects are quite small.

Key retention paths of a lambda with no closure

Worth knowing

In the pre-Roslyn-compiler era (Visual Studio 2013 and earlier), a lambda without a closure would compile into a static method inside the same class that calls the lambda. For the sake of performance, a separate class is now created. You can find more details in this Stack Overflow discussion.

The main takeaway

If you’re curious what a ...+<>c... object makes in your snapshot, check the memory traffic. If the object is created only once and is retained via static references, then it’s definitely a lambda without a closure. Should you worry? Absolutely not. This is normal compiler behavior.

A lambda with a closure


Of course, a more universal approach would be to make the maximum string length a variable:

Decompiled code

But how will this be compiled?

Here the compiler acted differently. It still created a separate class <>c__DisplayClass0__0, but the class no longer contains static fields. The length argument passed to a lambda is made a public field of the <>c__DisplayClass0__0 class. And what is most important, an instance of <>c__DisplayClass0__0 is created each time the FilterLongString() is called:

FilterTestClosureInArg.<>c__DisplayClass0_0 cDisplayClass00 = new FilterTestClosureInArg.<>c__DisplayClass0_0();

This results in huge memory traffic if the FilterLongString() method stays on a hot path (is called frequently). Let’s check this out in dotMemory.

What about dotMemory?

If we look at the Memory Traffic view, we’ll see that there were 10,000 instances of the <>c__DisplayClass0__0 class created and collected.

Memory traffic of a lambda with closure

This is not good from the performance point of view. While object allocation costs almost nothing, object collection is a much more “heavyweight” operation. The only way to avoid this is to rewrite the code so that it doesn’t contain closures. If you want more examples on the topic, take a look at our previous post, Unusual Ways of Boosting Up App Performance. Lambdas and LINQs.

Note that a closure is created in all cases where you pass local context. For example, a local variable passed to a lambda leads to a closure as well:

Worth knowing

LINQ queries have the same implementation under the hood, so the ...+<>c__DisplayClass... classes may indicate not only lambdas with closures but LINQs as well.

The main takeaway

When inspecting a snapshot, it always worth it to take a look at memory traffic. If you see a lot of allocated/collected objects with ...<>c__DisplayClass... in their names, you’ll know these are lambdas with closures. When examining these objects, ask yourself two questions:

  1. Do they really impact performance?
  2. If yes, can you rewrite the code without a closure?

A method group instead of a lambda


Back in 2006, C# 2.0 introduced the ‘method group conversion’ feature, which simplifies the syntax used to assign a method to a delegate. In our case, this means that we can replace the lambda with a method and then pass this method as an argument.

Decompiled code

We want to know if this approach will generate memory traffic. A quick look at the decompiled code says that yes, unfortunately, it will.

The compiler doesn’t create an additional class. Instead, it simply creates a new instance of Func<string, bool> each time FilterLongString() is called. Called 10,000 times, it will spawn 10,000 instances.

Memory traffic from a method group

The main takeaway

When using method groups, exercise the same caution as when you use lambdas with closures. They may generate significant memory traffic if on a hot path.

We hope this post has made things a little clearer. Now, if you encounter classes with ...+<>c... in their names, first of all, know that these are either lambdas or LINQs and the potential problem here is related to memory traffic because of closures.

Still don’t have dotMemory, but want to check your application for memory traffic? You’re welcome to download and try dotMemory free for 5 days of actual use.

(Note that the easiest way to install dotMemory is to use our Toolbox App. All you need to do is click Install next to dotMemory Portable.)

This entry was posted in How-To's and tagged , , . Bookmark the permalink.

4 Responses to What Do These …<>+c… Classes Do in my Memory Snapshots?

  1. Oleksii Vynnychenko says:

    where you pass la ocal context

  2. Pingback: Dew Drop – January 24, 2019 (#2884) | Morning Dew

  3. Steven Evans says:

    In the group method example Compare could be made static will this have an impact?

Leave a Reply

Your email address will not be published. Required fields are marked *