SilverLight - Memory Leak

时间:2021-11-08 03:48:35

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/

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 SDKs\Silverlight\v5.0\Toolkit\dec11\Source

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 SDKs\Silverlight\v5.0\Toolkit\dec11\Testing\*) to the Binaries folder( C:\Program Files (x86)\Microsoft SDKs\Silverlight\v5.0\Toolkit\dec11\Source\Source code\Binaries).

13. Replace the framework references (C:\Program Files (x86)\Microsoft SDKs\Silverlight\v5.0\Toolkit\dec11\Bin)

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

15. OK.