SilverLight

There is a memory leak issue in current silverlight project. 

It occurs in the search function: the memory usage of corresponding IE process increase about 30MB per search, and never get down.

Here is the way I found the bug and fix it:

1. download Windbg from official site:http://www.windbg.org/ 

when debug .net application, remember to load clr

If it is a 3.5 or lower .NET then you have to load sos by calling ".loadby sos mscorwks". If it is a 4.0 then you have to use ".loadby sos clr".

2. Dump the heap by the command: !dumpheap -stat -type <my project namespace>

So many objects......

3. copy the object data to a text file with named first_seach.txt.

4. tpye command "g" to keep running. And execute the second search function.

5. Break it, and dump the heap again:  !dumpheap -stat -type <my project namespace> 

6. do same operation to get the third heap dump.

7. compare the three txt file, and find the object numbers rule.

8. Luckly, I found my custom data grid object number is the same with the search times. There is the smoking gun.

9. There are dragdrop and contextMenu in this data grid. And both them can cause memory leak: 

http://silverlight.codeplex.com/workitem/6206 

https://silverlight.codeplex.com/workitem/7089 

http://blog.thekieners.com/2010/02/11/simple-weak-event-listener-for-silverlight/ 

So the drag drop and contextMenu are the bullets.

10. follow the fix: open the source code which is already installed on local machine: C:Program Files (x86)Microsoft SDKsSilverlightv5.0Toolkitdec11Source 

11. replace the two bad file: ContextMenu.cs and dragdroptarget.cs. I uploaded both the two files here: http://files.cnblogs.com/crazyghostvon/Sourcecode.zip 

12. Build to release. The test project will build failed if you didn't copy the testing references (C:Program Files (x86)Microsoft SDKsSilverlightv5.0Toolkitdec11Testing*) to the Binaries folder( C:Program Files (x86)Microsoft SDKsSilverlightv5.0Toolkitdec11SourceSource codeBinaries).

13. Replace the framework references (C:Program Files (x86)Microsoft SDKsSilverlightv5.0Toolkitdec11Bin)

14. Replace the references of current project, delete it and re-add them all.

15. OK.

原文地址:https://www.cnblogs.com/crazyghostvon/p/memoryleak.html