Anatomy of a “Memory Leak”

后端 未结 15 1290
失恋的感觉
失恋的感觉 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:42

    I would define memory leaks as an object not freeing up all the memory allocated after it has completed. I have found this can happen in your application if you are using Windows API and COM (i.e. unmanaged code that has a bug in it or is not being managed correctly), in the framework and in third party components. I have also found not tiding up after using certain objects like pens can cause the issue.

    I personally have suffered Out of Memory Exceptions which can be caused but are not exclusive to memory leaks in dot net applications. (OOM can also come from pinning see Pinning Artical). If you are not getting OOM errors or need to confirm if it is a memory leak causing it then the only way is to profile your application.

    I would also try and ensure the following:

    a) Everything that implements Idisposable is disposed either using a finally block or the using statement these include brushes, pens etc.(some people argue to set everything to nothing in addition)

    b)Anything that has a close method is closed again using finally or the using statement (although I have found using does not always close depending if you declared the object outside the using statement)

    c)If you are using unmanaged code/windows API's that these are dealt with correctly after. (some have clean up methods to release resources)

    Hope this helps.

    0 讨论(0)
  • 2020-11-29 15:42

    If you need to diagnose a memory leak in .NET, check these links:

    http://msdn.microsoft.com/en-us/magazine/cc163833.aspx

    http://msdn.microsoft.com/en-us/magazine/cc164138.aspx

    Those articles describe how to create a memory dump of your process and how to analyze it so that you can first determine if your leak is unmanaged or managed, and if it is managed, how to figure out where it is coming from.

    Microsoft also has a newer tool to assist with generating crash dumps, to replace ADPlus, called DebugDiag.

    http://www.microsoft.com/downloads/details.aspx?FamilyID=28bd5941-c458-46f1-b24d-f60151d875a3&displaylang=en

    0 讨论(0)
  • 2020-11-29 15:43

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

    0 讨论(0)
提交回复
热议问题