Memory Profiler showing k__BackingField for Property names

Memory profiling for .NET developers

Moderators: Luke Jefferson, StephenC, AndrewH, melvyn.harbour, james.billings, Laura Morley, dene.boulton

Memory Profiler showing k__BackingField for Property names

Postby CliffCawley » Mon Mar 04, 2013 9:14 am

Hi!

I've been using ANTS Memory Profiler trying to trace a test leak I made.

I noticed that when viewing the Instance Retention Graph that instances that are assigned to Properties show up with a name as follows:

Code: Select all
<Class> k__BackingField


For standard member variables it shows the name of the variable instead.

Why can't it show the Property name? Is that a bug that it doesn't?

If not it makes it difficult to find the correct instance without manually checking each variable of that type!

Am I missing something?
CliffCawley
 
Posts: 1
Joined: Mon Mar 04, 2013 9:09 am
Location: Australia

Postby Brian Donahue » Tue Mar 05, 2013 1:29 pm

Thanks for contacting Red Gate.

Backing Fields are compiler-generated code that is necessary to create a private member to return when your code does not explicitly use a private member.

For instance, this class will have backing fields:
Code: Select all
public class FileBrowser {
public List<string> Files { get; set; }
}

This class will not have backing fields, because a private member for the property accessor has been provided:
Code: Select all
 public class FileBrowser
    {
        private List<string> m_fileList = new List<string>();
 public List<string> Files
        {
            get
            {
                return m_fileList;
            }
            set
            {
                m_fileList = value;
            }
        }
    }
If you want to be able to have your own variable names in the backing fields, you can write out your properties' get and set code explicitly instead of using the compiler-generated code you have with property {get;set;}.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am


Return to ANTS Memory Profiler 7

Who is online

Users browsing this forum: No registered users and 0 guests

cron