Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Is it possible to determine when the Garbage collection runs?

Posted on 2006-05-10
Medium Priority
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
  • 2
LVL 48

Accepted Solution

AlexFM earned 500 total points
ID: 16650291
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

devsolns earned 500 total points
ID: 16652145
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?

Expert Comment

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

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

Expert Comment

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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

578 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