Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short answer is: unless it has unmanaged resources (file handles etc) you don't need to worry.</p> <p>The long answer is a bit more involved.</p> <p>When .NET decides it wants to free up some memory, it runs the <em>garbage collector</em>. This looks for all the objects which are still in use, and marks them as such. Any local variable (in any stack frame of any thread) which may still be read counts as a <em>root</em> as do static variables. (In fact I believe that static variables are referenced via live Type objects, which are referenced via live AppDomain objects, but for the most part you can regard static variables as roots.)</p> <p>The garbage collector looks at each object referred to by a root, and then finds more "live" references based on the instance variables within those objects. It recurses down, finding and marking more and more objects as "live". Once it's finished this process, it can then look at all the <em>rest</em> of the objects and free them.</p> <p>That's a very broad conceptual picture - but it gets a lot more detailed when you think of the <em>generational</em> model of garbage collection, finalizers, concurrent collection etc. I strongly recommend that you read Jeff Richter's <a href="http://rads.stackoverflow.com/amzn/click/0735621632" rel="noreferrer">CLR via C#</a> which goes into this in a lot of detail. He also has a <a href="http://msdn.microsoft.com/en-us/magazine/bb985010.aspx" rel="noreferrer">two</a> <a href="http://msdn.microsoft.com/en-us/magazine/bb985011.aspx" rel="noreferrer">part</a> article (back from 2000, but still very relevant) if you don't want to buy the book.</p> <p>Of course all this doesn't mean you don't need to worry about memory usage and object lifetimes in .NET. In particular:</p> <ul> <li>Creating objects pointlessly <em>will</em> cost performance. In particular, the garbage collector is fast but not free. Look for <em>simple</em> ways to reduce your memory usage as you code, but micro-optimising before you know you have a problem is also bad.</li> <li>It's possible to "leak" memory by making objects reachable for longer than you intended. Two reasonably common causes of this are static variables and event subscriptions. (An event subscription makes the event handler reachable from the event publisher, but not the other way round.)</li> <li>If you use more memory (in a live, reachable way) than you have available, your app will crash. There's not a lot .NET can do to prevent that!</li> <li>Objects which use non-memory resources typically implement <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx" rel="noreferrer"><code>IDisposable</code></a>. You should call <code>Dispose</code> on them to release those resources when you're finished with the object. Note that this <em>doesn't</em> free the object itself - only the garbage collector can do that. The <code>using</code> statement in C# is the most convenient way of calling <code>Dispose</code> reliably, even in the face of an exception.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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