Why can .NET not have memory leaks?

前端 未结 16 1400
春和景丽
春和景丽 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条回答
  •  Happy的楠姐
    2020-12-07 16:57

    .NET can have memory leaks.

    Mostly, people refer to the Garbage Collector, which decides when an object (or whole object cycle) can be gotten rid of. This avoids the classic c and c++ style memory leaks, by which I mean allocating memory and not freeing it later on.

    However, many times programmers do not realize that objects still have dangling references and do not get garbage collected, causing a... memory leak.

    This is normally the case when events are registered (with +=) but not unregistered later on, but also when accessing unmanaged code (using pInvokes or objects that use underlying system resources, such as the filesystem or database connections) and not disposing properly of the resources.

提交回复
热议问题