Garbage Collection In Micorosoft

18
Garbage Collection Garbage Collection in in Microsoft .NET Microsoft .NET Smitha Natarajamurthy

Transcript of Garbage Collection In Micorosoft

Page 1: Garbage Collection In  Micorosoft

Garbage Collection Garbage Collection in Microsoft .NETin Microsoft .NET

Smitha Natarajamurthy

Page 2: Garbage Collection In  Micorosoft

In and out of Garbage Collector..In and out of Garbage Collector..

GC in .NET Framework

How GC works? And Garbage Collector Algorithms

Mark – Sweep GC

Generational GC

Finalization vs. Dispose

Some tips on being friendly to GC

Garbage Collection Visualizations

CLR Profiler

Process Explorer

So, what’s new in the CLR 4.0 GC? - "Background" Garbage

Collector.

Take AwayTake Away

Page 3: Garbage Collection In  Micorosoft

GC in .NET FrameworkGC in .NET Framework

Page 4: Garbage Collection In  Micorosoft

Stack vs. HeapStack vs. HeapThe Stack is more or less responsible for keeping track of what's executing in our code (or what's been "called"). 

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.).

Page 5: Garbage Collection In  Micorosoft

Here are our two golden rules:

Reference Type always goes on the Heap.

Value Types and Pointers always go where they were declared. 

How it is decided what goes where? How it is decided what goes where? (Huh?)(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;          }

          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;          }

Page 6: Garbage Collection In  Micorosoft

TerminologyTerminologyHow the objects are laid out in the manage heap.

Page 7: Garbage Collection In  Micorosoft

When/how the GC runsWhen/how the GC runs

System is low in memory

Memory is too much fragmented

After quanta of allocation

There are multiple types of Garbage Collector available:

Mark-Sweep GC

Generational GC

Copying GC

Reference Counting GC

Page 8: Garbage Collection In  Micorosoft

Mark – Sweep GCMark – Sweep GC

Page 9: Garbage Collection In  Micorosoft

GC – Save PointGC – Save Point

Page 10: Garbage Collection In  Micorosoft

Mark – Sweep in ActionMark – Sweep in Action

Page 11: Garbage Collection In  Micorosoft

CompactionCompaction

Page 12: Garbage Collection In  Micorosoft

Generational GCGenerational GC

Page 13: Garbage Collection In  Micorosoft
Page 14: Garbage Collection In  Micorosoft

public class MyClass : IDisposable{

public MyClass () { } public virtual void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if(!disposed) { if (disposing = true)

{ //Cleanup managed resources } //Cleanup unmanaged resources

} disposed = true; } ~ MyClass () { Dispose(false); }

}

Finalization vs. DisposeFinalization vs. Dispose

Page 15: Garbage Collection In  Micorosoft

Some tips on being friendly to GCSome tips on being friendly to GC

Avoid object pools

Do Not Use GC.Collect()

Be careful about un-intended object creation

Finalization

Be careful around large objects (>85000B):

Pinning is bad for GC Performance

Set reference to null as soon you can specially

before blocking

Weak References

Page 16: Garbage Collection In  Micorosoft

Garbage Collection Garbage Collection VisualizationsVisualizations

- CLR Profiler

- Process Explorer

Page 17: Garbage Collection In  Micorosoft

So, what’s new in the CLR 4.0 So, what’s new in the CLR 4.0 GC? "Background" Garbage GC? "Background" Garbage CollectorCollector.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.

Page 18: Garbage Collection In  Micorosoft

Thank You!