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

后端 未结 5 1803
眼角桃花
眼角桃花 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:00

    The finalizer isn't deterministically called, so beware of using it to track things in a reliable way. If you remove the finalizer and instead use a WeakReference you should be able to determine whether the object was collected.

    All memory profilers should be able to find an issue such as this, but with varying degree of difficulty. I have personally used ANTS which is very easy yo use, but not free. It will help you show a reference diagram to the Foo instance, all the way from a GC root object. Seeing this diagram it is usually easy to spot who is holding the reference.

提交回复
热议问题