Why can .NET not have memory leaks?

前端 未结 16 1435
春和景丽
春和景丽 2020-12-07 16:17

Ignoring unsafe code, .NET cannot have memory leaks. I\'ve read this endlessly from many experts and I believe it. However, I do not understand why this is so.

It is

16条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 17:16

    Due to garbage collection, you can't have regular memory leaks (aside from special cases such as unsafe code and P/Invoke). However, you can certainly unintentionally keep a reference alive forever, which effectively leaks memory.

    edit

    The best example I've seen so far of a genuine leak is the event handler += mistake.

    edit

    See below for an explanation of the mistake, and of the conditions under which it qualifies as a genuine leak as opposed to an almost-genuine leak.

提交回复
热议问题