What are the most common (and often overlooked) causes of memory leaks in managed (.net) applications?

后端 未结 11 971
闹比i
闹比i 2021-02-04 09:32

Please can anyone recommend a quick checklist / best practice guide to help us avoid simple (but subtle) mistakes that cause could cause memory leaks in .net apps

I find

11条回答
  •  -上瘾入骨i
    2021-02-04 09:48

    The short answer is non-obvious references.

    To add some details:

    • Statics are not collected until AppDomain is collected (which may equal process shutdown)
    • Events (remember to unsubscribe)
    • Blocked finalizers (finalizers are run sequentially so any blocking finalizer will prevent all other finalizable objects from being collected). Example includes finalizers that can't get to a STA thread.
    • Deadlocked thread will never release roots
    • Forgetting to call Monitor.Exit() (e.g. when used with a timeout or across methods) may cause a deadlock which in turn may cause a leak

提交回复
热议问题