Tuesday, September 27, 2011

How to know when .Net GC is working

One of my previous posts I was explaining how to learn the working of JIT during an application run. That gives us actual feeling of how the concept if working in the real world. If you are interested to know how the other concepts are working you can monitor using the windows tool called perfmon.

Perfmon.exe can be found inside the system32 folder in Windows installation

<InstallDrive>:\Windows\System32\perfmon.exe

You can easily open the application by just typing perfmon in the search box of start menu.

Using Perfmon to monitor the .net Activities

Perfmon is a great tool and its not possible to cover all the aspects of it in this post. Just google for its full usage scenarios. I am just concentrating on how to use it to monitor when GC is working for our application. You can use the same method to know when the jitting happens.

Ok. Lets open the perfmon and watch what are the options in it.There are PerformanceMonitor, ReliabilityMonitor, Data collector sets and Reports.We are interested in the performance monitor where we get the status of GC using a counter.What is a counter in perfmon?Counter is a criteria or specification what to monitor which can be added to a graph to see its current value. By default the processor time counter will be available there. Below are the easy steps to add a counter for monitoring GC of an application.

Goto Performance monitor->Click ”+” button at top –>Expand “.Net CLR Memory” in the available counters->Select “# Gen 0 Collections”->Select your application in the “Instances of selected object” section->Click on “Add” –>Click “Ok”

Remove the processor time counter for easy monitoring. Now the graph will reflect the change when GC comes into picture. For learning purpose better put a button to invoke GC using GC.Collect method .So that you can see the counter increments when you click on that button.

When you add the counter you might have noticed so many counters available there .Play with those and conquer the technology with its inner working.

No comments: