Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting number of GC cleanups on an object

How do I keep a count of the number of times that objects of a specific class (type?) are getting disposed in the lifetime of my application. Imagine I have a class A, now, I want to count how many times the objects of A get collected by the GC.

I hope I am phrasing this right because I was asked this in an interview today and the answer I gave did not satisfy the interviewer. And this is what I imagine he was trying to ask.

What I said was that one could keep a static field called count in the class A and increment it in the Finalize() call of that object.

The answer he was expecting was something called a static block. I've never heard of this in .NET/C#. Can someone explain what's this static block?

like image 546
tsps Avatar asked Nov 14 '22 11:11

tsps


1 Answers

Use a tool like memprofiler, Redgate Ants, dotTrace, CLR Profiler (needs admin rights for .NET 3.5 apparently) or the team edition of Visual Studio.

like image 107
Mitch Wheat Avatar answered Dec 04 '22 11:12

Mitch Wheat