Anatomy of a “Memory Leak”

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

    Strictly speaking, a memory leak is consuming memory that is "no longer used" by the program.

    "No longer used" has more than one meaning, it could mean "no more reference to it", that is, totally unrecoverable, or it could mean, referenced, recoverable, unused but the program keeps the references anyway. Only the later applies to .Net for perfectly managed objects. However, not all classes are perfect and at some point an underlying unmanaged implementation could leak resources permanently for that process.

    In all cases, the application consumes more memory than strictly needed. The sides effects, depending on the ammount leaked, could go from none, to slowdown caused by excessive collection, to a series of memory exceptions and finally a fatal error followed by forced process termination.

    You know an application has a memory problem when monitoring shows that more and more memory is allocated to your process after each garbage collection cycle. In such case, you are either keeping too much in memory, or some underlying unmanaged implementation is leaking.

    For most leaks, resources are recovered when the process is terminated, however some resources are not always recovered in some precise cases, GDI cursor handles are notorious for that. Of course, if you have an interprocess communication mechanism, memory allocated in the other process would not be freed until that process frees it or terminates.

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

    All memory leaks are resolved by program termination.

    Leak enough memory and the Operating System may decide to resolve the problem on your behalf.

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

    The best explanation I've seen is in Chapter 7 of the free Foundations of Programming e-book.

    Basically, in .NET a memory leak occurs when referenced objects are rooted and thus cannot be garbage collected. This occurs accidentally when you hold on to references beyond the intended scope.

    You'll know that you have leaks when you start getting OutOfMemoryExceptions or your memory usage goes up beyond what you'd expect (PerfMon has nice memory counters).

    Understanding .NET's memory model is your best way of avoiding it. Specifically, understanding how the garbage collector works and how references work — again, I refer you to chapter 7 of the e-book. Also, be mindful of common pitfalls, probably the most common being events. If object A is registered to an event on object B, then object A will stick around until object B disappears because B holds a reference to A. The solution is to unregister your events when you're done.

    Of course, a good memory profile will let you see your object graphs and explore the nesting/referencing of your objects to see where references are coming from and what root object is responsible (red-gate ants profile, JetBrains dotMemory, memprofiler are really good choices, or you can use the text-only WinDbg and SOS, but I'd strongly recommend a commercial/visual product unless you're a real guru).

    I believe unmanaged code is subject to its typical memory leaks, except that shared references are managed by the garbage collector. I could be wrong about this last point.

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

    I found .Net Memory Profiler a very good help when finding memory leaks in .Net. It's not free like the Microsoft CLR Profiler, but is faster and more to the point in my opinion. A

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

    The best explanation of how the garbage collector works is in Jeff Richters CLR via C# book, (Ch. 20). Reading this gives a great grounding for understanding how objects persist.

    One of the most common causes of rooting objects accidentally is by hooking up events outisde a class. If you hook up an external event

    e.g.

    SomeExternalClass.Changed += new EventHandler(HandleIt);
    

    and forget to unhook to it when you dispose, then SomeExternalClass has a ref to your class.

    As mentioned above, the SciTech memory profiler is excellent at showing you roots of objects you suspect are leaking.

    But there is also a very quick way to check a particular type is just use WnDBG (you can even use this in the VS.NET immediate window while attached):

    .loadby sos mscorwks
    !dumpheap -stat -type <TypeName>
    

    Now do something that you think will dispose the objects of that type (e.g. close a window). It's handy here to have a debug button somewhere that will run System.GC.Collect() a couple of times.

    Then run !dumpheap -stat -type <TypeName> again. If the number didn't go down, or didn't go down as much as you expect, then you have a basis for further investigation. (I got this tip from a seminar given by Ingo Rammer).

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

    I will concur with Bernard as to in .net what a mem leak would be.

    You could profile your application to see its memory use, and determine that if its managing a lot of memory when it should not be you could say it has a leak.

    In managed terms I will put my neck on the line to say it does go away once the process is killed/removed.

    Unmanaged code is its own beast and if a leak exists within it, it will follow a standard mem. leak definition.

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