Recently, I have been doing a lot of Memory troubleshooting.
Here are some windbg commands that helped:
Load the sos module (for .Net 4.0)
.loadby sos clr
List all of my object specific to my program:
!dumpheap -stat -type <parent namespace>
List all the instances of a particular object:
!dumpheap -type <namespace>.<object class name>
List references to the object:
!gcroot <address>
Break on garbage collection:
!findroots gen any
List references to the obect (must break in garbage collection):
!findroots <address>
Show sizes in the memory:
!eeheap
List large strings in memory:
!dumpheap -type System.String -min 10000
Then, I also found that you can use the SOS module with within Visual Studio. First, in the project settings, I had to go to the Debug tab and check “Enable unmanaged code debugging” under “Enable Debuggers”. Then, in the immediate window, I could run the following command:
.load sos
I thought my problem seemed to be related to the Data Grid view just like this forum post. The post lists a code fix, but my problem is finding out where I place the code. This blog mentions putting the code in the Form Close, but my problem is during the execution of the program.
Actually, it ended up being that I was loading a hidden column into the grid that was taking a lot of memory. I had a Select * SQL statement, and I had to list only the fields that I wanted in memory.
Resources
- THE RIGHT STUFF: WinDbg Commands
- Delay’s Blog: Where’s your leak at? [Using WinDbg, SOS, and GCRoot to diagnose a .NET memory leak]
- MSDN: SOS.dll (SOS Debugging Extension)
- Debugging + Random Stuff: Interesting WinDbg Extension SOS commands in CLR 4.0/.NET Framework 4.0 CTP, .NET runtime dll renamed and sos commands just got richer
- If broken it is, fix it you should: New commands in SOS for .NET 4.0 Part 1
- MSDN: How to: Use SOS
UserPreferenceChangedEventHandler Resources
- Windows Developer Center Forums: UserPreferenceChangedEventHandler and Object Retention
- .Net and Memory: Memory Leak in ToolStripTextBoxControl
- .Net Memory Profiler Forum: Memory Leak on Datagridview
- MSDN Blogs: How to: Respond to Font Scheme Changes in a Windows Forms Application
- Hacking Code: Unhooking a leaking C# object from Microsoft.Win32.SystemEvents.UserPreferenceChanged