Are memory leaks possible in managed environments like .NET?

后端 未结 7 1300
梦谈多话
梦谈多话 2021-01-05 02:58

In C++ it is easily possible to have a permanent memory leak - just allocate memory and don\'t release it:

new char; //permanent memory leak guaranteed
         


        
7条回答
  •  庸人自扰
    2021-01-05 03:20

    Easiest memory leak:

    public static class StaticStuff
    {
        public static event Action SomeStaticEvent;
    }
    
    public class Listener
    {
       public Listener() {
          StaticStuff.SomeStaticEvent+=DoSomething;
       }
       void DoSomething() {}
    }
    

    instances of Listener will never be collected.

提交回复
热议问题