Is it possible to determine when the Garbage collection runs?

Posted on 2006-05-10
Last Modified: 2010-04-16
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#?
Question by:neo-the-one
    LVL 48

    Accepted Solution

    Garbage collector runs only when application consumes almost all available memory, this doesn't happen every second. To make presize time management for functions you can use Stopwatch class (VS 2005).
    Notice that Windows is not real time system, it can always make thread context switch, this makes execution time unpredictable. You always need to count average time, don't beleive to single result.
    LVL 13

    Assisted Solution

    If you explicity want to know when the GC runs then use the .NET CLR profiler and the .NET performance counters in your system tools.  They tell you EXACTLY what is getting gc'ed, whate generation ect.  The performance counters allow you to profile ANYTHING including odd things like when finally blocks are called.

    From your post above I can assure you that the GC is NOT causing that delay.  What is the code doing exactly?
    LVL 2

    Expert Comment

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

    // Usage
    DetectGC gdc = new DetectGC();
    gdc = null;
    LVL 13

    Expert Comment

    yes but doesnt scale.  you'll find much more valuble info in the CLR profiler and perfmons.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now