Understanding Total vs Held instances

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
axel.isr
Posts: 1
Joined: Thu Jan 31, 2019 2:10 pm

Understanding Total vs Held instances

Post by axel.isr » Thu Jan 31, 2019 2:24 pm

Hi,

I'm testing Memory Profiler with an app at my company, so far so good.
I found in my app that I have a lot of difference between Total and Held instances (sometimes 1 vs millions). Is this a problem I should be taking care of? I've read documentation but didn't find anything about this "issue".

Here's an example:
Image

Image

Thanks!

Andreas Suurkuusk
Posts: 1025
Joined: Wed Mar 02, 2005 7:53 pm

Re: Understanding Total vs Held instances

Post by Andreas Suurkuusk » Mon Feb 04, 2019 7:40 am

"Held instances" are instances that are prevented from being garbage collected by the item (instance, type, filter) you are investigating. So you can have a single instance that have many "held instances".

You can read more about held instance in the online documentation at https://memprofiler.com/online-docs/hel ... tances.htm.

The small example program below can be used to illustrate held instances.

Code: Select all

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class SharedData
    {
        int[] data = new int[10000];
    }

    class SomeDataBase
    {
        int a;
        int b;
        SharedData sharedData;

        protected SomeDataBase(SharedData sharedData)
        {
            this.sharedData = sharedData;
        }

    }

    class SomeData1 : SomeDataBase
    {
        int c;
        int d;

        internal SomeData1(SharedData sharedData) : base(sharedData)
        {
        }
    }

    class SomeData2 : SomeDataBase
    {
        int e;
        int f;

        internal SomeData2(SharedData sharedData) : base(sharedData)
        {

        }
    }

    class SomeContainer
    {
        internal List<SomeDataBase> allData = new List<SomeDataBase>();

    }

    class Program
    {
        static SomeContainer container;

        static void Main(string[] args)
        {
            Init();
            Console.ReadLine();
        }

        private static void Init()
        {
            var sharedData = new SharedData();
            Program.container = new SomeContainer();

            for (int i = 0; i < 1000; i++)
            {
                if ((i % 2) == 0)
                {
                    Program.container.allData.Add(new SomeData1(sharedData));
                }
                else
                {
                    Program.container.allData.Add(new SomeData2(sharedData));
                }
            }

        }
    }
}
The screenshot below shows the instance information for the "ConsoleApp1" classes:
HeldInstances.png
Here you can see that the SomeContainer type with only a single instance holds 1,005 instances. These are the 500 SomeData1 instances, the 500 SomeData2 instances, the List<SomeDataBase>, the SomeDataBase[] array (used by the list), the SharedData instance, the int[] array used by SharedData, and the SomeContainer instance itself.

In the screenshot you can also see the information for a filter I created ("Derived from 'ConsoleApp1.SomeDataBase'"). The instances in this filter holds 1,002 instances, even if SomeData1 and SomeData2 only hold 500 instances each. This is caused by the fact that the SharedData instance (and its int[] array) is held by the SomeData1 and SomeData2 instances, but only when considering all instances at once.

I hope this helps.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Arrora
Posts: 1
Joined: Sat Jun 08, 2019 8:40 am

Re: Understanding Total vs Held instances

Post by Arrora » Tue Jun 11, 2019 2:08 pm

Can the filter you created be modified in any way, Andreas? Great explanations by the way.

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests