I am doing analysis on the time spend in a critical section of code.
//Take a timestamp before
var before = DateTime.UtcNow;
DoCriticalMethod();
//Take a timestamp after
var after = DateTime.UtcNow;
And sometime I have some values clearly higher than others.
I suspect that it is Garbage collection, so I want to correlate the high values with the fact that a Garbage collection occured during the process.
I tried this so far : I take the counter before :
//Take the number before
int gc2CountBefore = GC.CollectionCount(2);
...
//Take the number after
bool hasgc2occured = (GC.CollectionCount(2) - gc2CountBefore) != 0;
Am I doing it in the good way?