Link to home
Start Free TrialLog in
Avatar of neo-the-one
neo-the-one

asked on

Is it possible to determine when the Garbage collection runs?

I am analyzing my code(in a GUI app) to determine in which step the delay is occuring. To do so, I am printing out the time stamp in "ss.fff" format to print the milliseconds. What i found is that there is more than 100 ms delay occuring in the 10 steps of the code. The delay is not occuring at the same point all the time. Sometimes the delay occurs between steps 2 and 3, sometime between 6 &7 and sometimes in between 9 & 10. Also the delay is not always constant and varies from 100 to 400 ms. The entire code is in a single method and is accessing objects existing in the current thread. I m suspecting that the garbage collection might be running in between. Is it possible to figure out when the garbage collection occurs in C#?
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of __alex
__alex

class DetectGC
{
    ~DetectGC() {
        MessageBox.Show("Garbage collected."); // Runs on finalization thread!
    }
}

// Usage
DetectGC gdc = new DetectGC();
gdc = null;
GC.Collect();
yes but doesnt scale.  you'll find much more valuble info in the CLR profiler and perfmons.