- Installation
- Feature Set
- Writing Your Own Plugs
- Future Releases
- Summary
Writing Your Own Plugs
Perhaps the real power of Debug Analyzer.NET comes from the fact that anyone can write his or her own plugs using a neat .NET API. In this part of the article, we’ll write a simple plug that enumerates all objects on the managed heap and displays the top five occupants by type and the top five instances for each type. The overall process for writing a plug is as following:
- Create a new class library using your favorite .NET language (I will be using C# for this example).
- Add a reference to the Debug.Analyzer.API.dll assembly.
- Include the following using statement:
- Define a new class that derives from AnalysisPlug.
- Override the RunAnalysis method.
- Provide your plug code in the RunAnalysis method.
- Build and deploy the resulting assembly to the Analysis folder.
using DebugAnalyzer.ObjectModel;
Listing 1 shows the skeletal version of our custom plug.
Listing 1Skeletal Implementation of a Custom Plug
using System.Text; using DebugAnalyzer.ObjectModel; namespace HeapAnalysis { public class AnalyzeHeap : AnalysisPlug { public override void RunAnalysis() { // Plug code } } }
Before we delve into the details of our particular plug, one critical point to mention is that the Debug Analyzer.NET object model is rooted in a class called Analyzer. Using properties and methods of that class, we can dig deeper into all the aspects of the runtime. For example, if we wanted to get collection of CLR heaps, we can use the following code:
foreach (ClrHeap h in Analyzer.CLR.ClrHeapCollection) { // Use each heap ‘h’ }
Back to our particular plug: The first task we need to complete is to find all objects on the managed heap that occupies the biggest size and grab a collection of five of those instances. We can do that by using the following code:
foreach (ClrHeap h in Analyzer.CLR.ClrHeapCollection.OrderByDescending(h => h.TotalSize).Take(5)) { IEnumerable<ClrObject> objects = Analyzer.CLR.GetClrObjectsByMT(h.MethodTable); }
The objects variable now can be used to iterate over the objects and output the object information in HTML format to the report as shown below:
foreach (ClrHeap h in Analyzer.CLR.ClrHeapCollection.OrderByDescending(h => h.TotalSize).Take(5)) { IEnumerable<ClrObject> objects = Analyzer.CLR.GetClrObjectsByMT(h.MethodTable); foreach (ClrObject obj in objects.Take(5)) { WriteHtmlLine(obj.ToHtml()); // Output object like !sos.dumpobject } }
I strongly encourage you to use the Instant Analysis feature while developing your plug in order to get instant feedback and minimize the amount of time you spent on building, copying the plug assembly, and testing it.