Ramya
Ramya

Reputation: 677

Find when Garbage Collector last ran in .NET

Is there a way to figure out when the Garbage collector last ran in my program? Suppose I am running a computation-intensive program and am interested whether GC ran in the middle, how do I do that?

Thanks in advance.

Upvotes: 2

Views: 304

Answers (3)

Lepton
Lepton

Reputation: 37

Try CLR Profiler

I used it for .NET 3.5 (should be ok for 2.0-3.5)

http://msdn.microsoft.com/en-us/library/ff650691.aspx

For .NET 4.0

http://www.microsoft.com/download/en/details.aspx?id=16273

Upvotes: 1

Justin
Justin

Reputation: 86779

You can probably use Permon counters for this, specifically the .Net CLR Memory -> % Time in GC value:

% Time in GC is the percentage of elapsed time that was spent in performing a garbage collection (GC) since the last GC cycle. This counter is usually an indicator of the work done by the Garbage Collector on behalf of the application to collect and compact memory. This counter is updated only at the end of every GC and the counter value reflects the last observed value; its not an average.

Its not exactly what you are asking for, but it looks to be a reasonable indication of the cost of garbage collection. As a bonus, if the value of this counter changes then it means that at least one GC cycle has taken place.

Upvotes: 0

SLaks
SLaks

Reputation: 887767

You could add logging code to the finalizer of an object that got collected.
This should only be done for diagnostic purposes; finalizers should not be used except for native resources.

You can also look at the GC performance counters; for more details, see here.

Upvotes: 2

Related Questions