Anatomy of a “Memory Leak”

后端 未结 15 1330
失恋的感觉
失恋的感觉 2020-11-29 14:41

In .NET perspective:

  • What is a memory leak?
  • How can you determine whether your application leaks? What are the effects?
  • How can you prevent a
15条回答
  •  情深已故
    2020-11-29 15:34

    I guess in a managed environment, a leak would be you keeping an unnecessary reference to a large chunk of memory around.

    Absolutely. Also, not using the .Dispose() method on disposable objects when appropriate can cause mem leaks. The easiest way to do it is with a using block because it automatically executes .Dispose() at the end:

    StreamReader sr;
    using(sr = new StreamReader("somefile.txt"))
    {
        //do some stuff
    }
    

    And if you create a class that is using unmanaged objects, if you're not implementing IDisposable correctly, you could be causing memory leaks for your class's users.

提交回复
热议问题