In order to track down memory leaks, I have been using Visual Studio and SOS.dll to provide dumps of the object heap, and to see what is GCRooting a particular object in place.
I started automating this process using AutoIt to control my mouse and keyboard. Here is an approximation of the first part of my script:
In Visual Studio, press F5 to start execution
Let the program fully load
Switch to Visual Studio and break executionSwitch focus to the immediate window
Load SOS.dll
Call!dumpheap -stat
Copy the result of the dump to the clipboard
Paste in notepad and save as dump1.txtResume execution
Perform the leaky action a number of times
Switch to Visual Studio and break executionSwitch focus to the immediate window
Clear the output
Call!dumpheap -stat
Copy the result of the dump to the clipboard
Paste in notepad and save as dump2.txtRun a Python script which compares the two dumps and locates leaked objects
I have a few concerns with my current method, though. Not only am I dependent on an installation of Visual Studio, but while the script is running I cannot use the mouse or keyboard without throwing the automation out of sequence.
Is there a way to use SOS.dll within a C# program rather than Visual Studio? Ideally this solution would attach to a specified process and output the result of !dumpheap -stat
to a text file without the need for automating Visual Studio.