Viewing garbage collection history in c# (VS2015)
c#, garbage, garbage-collection, visual-studio-2015
Solution
Pretty much any memory profiler will show this info. Just look for a list of "Dead objects" between two snapshots and that is the list of "garbage" that was generated and will need to be collected by the GC.
I personally use DotMemory by JetBrains.
For example with the following program
using System;
namespace SandboxConsole
{
class Program
{
private int _test;
static void Main(string[] args)
{
var rnd = new Random();
while (true)
{
var obj = new Program();
obj._test = rnd.Next();
Console.WriteLine(obj);
}
}
public override string ToString()
{
return _test.ToString();
}
}
}
It gave me a output like
So you can see between the two snapshots (that where about 5 seconds apart) 218,242 strings, char[]s, and Program objects where collected by the garbage collector. and by clicking on strings we can see the call stacks where the objects where created. (note you do need to enable the "collect allocation data" option to see those call stacks, without it you get the total numbers but not where the objects came from)
Problem
A unforeseen and unanticipated amount of garbage collection activity is shown in the 'Process Memory' graph when I run my application which makes me want to know where in the program is the garbage generated as I don't feel that I have any memory leaks in the program. Can someone please tell me if there is a way to view the parts (or lines) of my code where garbage is generated? Thanks in advance.