Typically, if you have a leak in a managed application, this means that something is not going to. Common sources include
Event handlers. If the subscriber is not deleted, the publisher will continue it.
Statics
Finalizers: A locked finalizer will prevent the finalizer thread from starting up with any other finalizers, and thus prevent the collection of these instances.
Similarly, a deadlock thread will be supported by any roots that it executes. Of course, if you have deadlock threads that are likely to affect the application at several levels.
To fix this problem, you need to check the managed heap. WinDbg + SOS (or PSSCOR) will allow you to do this. The !dumpheap -stat displays the entire managed heap.
You need to have an idea of ββthe number of instances of each type expected on the heap. When you find something strange, you can use the command !dumpheap -mt <METHOD TABLE> to display all instances of a given type.
The next step is to analyze the root of these instances. Choose one randomly and do !gcroot . This will show how this particular instance is being implemented. Look at event handlers and pinned objects (usually static links). If you see the finalizer queue, you need to learn what the finalizer stream does. To do this, use the !clrstack and !clrstack .
If everything looks fine for this instance, you move on to another instance. If this does not work, you may need to come back to look at the pile again and repeat from there.
Other sources of leaks include: assemblies that are not unloaded and fragmentation of the heap of a large object. SOS / PSSCOR can help you find them, but for now I will not list the details.
If you want to know more, I recommend the Tess blog . I also made some videos about using WinDbg + SOS ( here and here ).
If you have the ability to debug a process while it is starting, I recommend PSSCOR instead. PSSCOR is essentially a private SOS source branch that has been complemented by additional commands, and many of the existing SOS commands have also been improved. For example. The PSSCOR version of the !dumpheap has a very useful delta column, which greatly simplifies troubleshooting for memory leaks.
To use it, you need to start your process, connect WinDbg and download PSSCOR and execute !dumpheap -stat . Then you start the process again so that the distributions are made. Break the execution and reissue the command. Now PSSCOR will show you the number of instances that have been added / removed since the previous inspection.
Brian Rasmussen Mar 01 '12 at 16:12 2012-03-01 16:12
source share