Garbage Collection unter.NET Garbage Collection Optimierung Finalization Resurrection.
Garbage Collection In Micorosoft
-
Upload
smithanatarajamurthy -
Category
Documents
-
view
1.266 -
download
0
Transcript of Garbage Collection In Micorosoft
Garbage Collection Garbage Collection in Microsoft .NETin Microsoft .NET
Smitha Natarajamurthy
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
GC in .NET FrameworkGC in .NET Framework
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.).
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; }
TerminologyTerminologyHow the objects are laid out in the manage heap.
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
Mark – Sweep GCMark – Sweep GC
GC – Save PointGC – Save Point
Mark – Sweep in ActionMark – Sweep in Action
CompactionCompaction
Generational GCGenerational GC
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
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
Garbage Collection Garbage Collection VisualizationsVisualizations
- CLR Profiler
- Process Explorer
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.
Thank You!