Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the "what is a memory leak" and "what are the effects" questions have been answered well already, but I wanted to add a few more things on the other questions...</p> <p><strong>How to understand whether your application leaks</strong></p> <p>One interesting way is to open <em>perfmon</em> and add traces for <em># bytes in all heaps</em> and <em># Gen 2 collections</em> , in each case looking just at your process. If exercising a particular feature causes the total bytes to increase, and that memory remains allocated after the next Gen 2 collection, you might say that the feature leaks memory.</p> <p><strong>How to prevent</strong></p> <p>Other good opinions have been given. I would just add that perhaps the <em>most commonly overlooked</em> cause of .NET memory leaks is to add event handlers to objects without removing them. An event handler attached to an object is a form of reference to that object, so will prevent collection even after all other references have gone. Always remember to detach event handlers (using the <code>-=</code> syntax in C#).</p> <p><strong>Does the leak go away when the process exits, and what about COM interop?</strong></p> <p>When your process exits, all memory mapped into its address space is reclaimed by the OS, including any COM objects served from DLLs. Comparatively rarely, COM objects can be served from separate processes. In this case, when your process exits, you may still be responsible for memory allocated in any COM server processes that you used.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload