Debugging .NET memory leaks - how to know what is holding a reference to what?

后端 未结 5 1781
眼角桃花
眼角桃花 2021-01-02 13:12

I am working on a .NET application where there appears to be a memory leak. I know the text-book answers, that events should be unsubscribed, disposable objects should be di

5条回答
  •  时光说笑
    2021-01-02 14:09

    Have a look at the SOS debugger extension (It's free, an can be used within Visual Studio).

    You may find this and this helpful to get startet.

    If you have succefully set up SOS (this can be tricky sometimes), knowing what holds a reference to what is as easy as

    // load sos
    .load sos
    // list of all instances of YourTypeName in memory with their method tables
    !DumpHeap -type YourTypeName  
    // put here the method table displayed by the previous command
    // it will show you the memory address of the object
    !DumpHeap -mt 07f66b44              
    // displays information about references the object at the specified address
    !GCRoot 02d6ec94
    

提交回复
热议问题