I am breaking my head, but still I dint get solution. I ran these code and analyzed with .NET memory profiler. It showed me that one instance of IntEntity[] is not collected. But I am clearing the list and setting it to null. How I can make this to garbage collect? Am I doing anything wrong here?
Edit: I tried setting b = null & calling GC.Collect(GC.MaxGeneration); But same results.
Edit2: Added images from NET Memory Profiler & ANTS Memory profiler
Please help me.
Here is the code am using,
public class IntEntity
{
public int Value { get; set; }
}
public abstract class Base
{
protected List<IntEntity> numbers;
public Base()
{
}
public abstract void Populate();
public int Sum()
{
numbers = new List<IntEntity>();
Populate();
int sum = 0;
foreach (IntEntity number in numbers)
{
sum += number.Value;
}
numbers.Clear();
numbers = null;
return sum;
}
}
public class Child : Base
{
public override void Populate()
{
numbers.Add(new IntEntity() { Value = 10 });
numbers.Add(new IntEntity() { Value = 20 });
numbers.Add(new IntEntity() { Value = 30 });
numbers.Add(new IntEntity() { Value = 40 });
}
}
Base b = new Child();
MessageBox.Show(b.Sum().ToString());
b = null;
GC.Collect(GC.MaxGeneration);


As Jim Mische and Steven Sudit pointed out it may be that the GC may simply not be collecting because the RAM available to the runtime is greater than the amount of memory required by the program
You can add GC.Collect() after setting numbers to null and it should probably disappear from your profile.
You should note that typically you should only induce a Garbage Collection for testing purposes only.