How to detect where a Memory Leak is?

后端 未结 12 1743
感动是毒
感动是毒 2020-12-13 11:00

I have a large website that seems to be sucking up all the memory that is being allocated. There is nothing else on the server beside this site. Within a week it eats away t

12条回答
  •  一生所求
    2020-12-13 11:49

    Memory leaks in .NET are not that common, but when they happen it is most commonly due to unattached event handlers. Make sure you detach handlers, before the listeners go out of scope.

    Another option is if you forget to call Dispose() on IDisposable resources. This may prevent cleanup of unmanaged resources (which are not handled by GC).

    And yet another possible reason is a deadlocked finalizer. That will prevent all remaining objects in the finalizer queue from being collected.

    I use WinDbg + Sos to track down leaks. The steps are as follows

    • Dump the heap and look for suspects
    • Use !gcroot to find out what is keeping the suspects alive
    • Repeat as necessary

    Be aware that large memory usage may also be due to heap fragmentation. The regular heaps are compacted, but pinned objects can cause fragmentation. Also, the LOH is not compacted so fragmentation is not uncommon for LOH.

    Excellent tutorials on WinDbg + sos here: http://blogs.msdn.com/tess/

提交回复
热议问题