"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:
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.