Garbage Collection In Micorosoft


Published on

Dscribes about in and out of Garbage Collector. How the GC fits in .Net framework, its algorithm and some tips to being friendly with GC. Along with basic understanding of memory management in .Net (Stack vs. Heap). This also depicts about the GC visualization tools and CLR 4.0 GC – Back Ground garbage collector.

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Garbage Collection In Micorosoft

  1. 1. Garbage Collection in Microsoft .NET Smitha Natarajamurthy
  2. 2. <ul><li>In and out of Garbage Collector.. </li></ul><ul><li>GC in .NET Framework </li></ul><ul><li>How GC works? And Garbage Collector Algorithms </li></ul><ul><ul><li>Mark – Sweep GC </li></ul></ul><ul><ul><li>Generational GC </li></ul></ul><ul><li>Finalization vs. Dispose </li></ul><ul><li>Some tips on being friendly to GC </li></ul><ul><li>Garbage Collection Visualizations </li></ul><ul><ul><li>CLR Profiler </li></ul></ul><ul><ul><li>Process Explorer </li></ul></ul><ul><li>So, what’s new in the CLR 4.0 GC? - &quot;Background&quot; Garbage Collector. </li></ul>Take Away
  3. 3. GC in .NET Framework
  4. 4. Stack vs. Heap The Stack is more or less responsible for keeping track of what's executing in our code (or what's been &quot;called&quot;).  The Heap is more or less responsible for keeping track of our objects (our data, well... most of it - we'll get to that later.).
  5. 5. <ul><li>Here are our two golden rules: </li></ul><ul><ul><li>Reference Type always goes on the Heap. </li></ul></ul><ul><ul><li>Value Types and Pointers always go where they were declared.  </li></ul></ul>How it is decided what goes where? (Huh?)           public class MyInt           {                       public int MyValue;           } And the following method is executing:           public MyInt AddFive( int pValue)           {                 MyInt result = new MyInt();                 result.MyValue = pValue + 5;                 return result;           }
  6. 6. Terminology How the objects are laid out in the manage heap.
  7. 7. When/how the GC runs <ul><li>System is low in memory </li></ul><ul><li>Memory is too much fragmented </li></ul><ul><li>After quanta of allocation </li></ul><ul><li>There are multiple types of Garbage Collector available: </li></ul><ul><li>Mark-Sweep GC </li></ul><ul><li>Generational GC </li></ul><ul><li>Copying GC </li></ul><ul><li>Reference Counting GC </li></ul>
  8. 8. Mark – Sweep GC
  9. 9. GC – Save Point
  10. 10. Mark – Sweep in Action
  11. 11. Compaction
  12. 12. Generational GC
  13. 14. Finalization vs. Dispose <ul><li>public class MyClass : IDisposable </li></ul><ul><li>{ </li></ul><ul><ul><li>public MyClass () </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public virtual void Dispose() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>Dispose(true); </li></ul></ul><ul><ul><li> GC.SuppressFinalize(this); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>protected virtual void Dispose(bool disposing) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>if(!disposed) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>if (disposing = true) </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li> //Cleanup managed resources </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li> //Cleanup unmanaged resources </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>disposed = true; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>~ MyClass () </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>Dispose(false); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  14. 15. Some tips on being friendly to GC <ul><li>Avoid object pools </li></ul><ul><li>Do Not Use GC.Collect() </li></ul><ul><li>Be careful about un-intended object creation </li></ul><ul><li>Finalization </li></ul><ul><li>Be careful around large objects (>85000B): </li></ul><ul><li>Pinning is bad for GC Performance </li></ul><ul><li>Set reference to null as soon you can specially before blocking </li></ul><ul><li>Weak References </li></ul>
  15. 16. Garbage Collection Visualizations - CLR Profiler - Process Explorer
  16. 17. So, what’s new in the CLR 4.0 GC? &quot;Background&quot; Garbage Collector .NET 4.0 introduces Background GC . The Concurrent GC allocates memory while running but in the current segment which is 16 MB on a workstation. After that all threads are suspended. The Background GC allows a separate ephemeral GC - gen0 and gen1 - to be started while the full GC - gen0, 1, and 2 - is running and that means access to another memory segment. CLR 4.0 changes that to support background collection, which can do a Generation 0 and Generation 1 collection at the same time as a Generation 2 collection. This means that now only unusual circumstances should lead to long latency times.
  17. 18. Thank You!