5. Summer School 2012
Manual vs. Automatic
• Fast • Easy to program
• Precise • Keeps you focused
• Deterministic • Heuristically optimized
BUT BUT
• Error-prone • Unpredictable
• Distracting • Causes noticeable delays
• Hard to debug • Sometimes leaks as well
17. Summer School 2012
.NET CLR Managed Heap
NextObjPtr
Managed
Object A Object B Object C Object D Object E Object F
heap
• Items are allocated consecutively.
• The only allocation overhead is about incrementing the pointer!
• When objects are destroyed, the managed heap gets automatically
condensed (except the Large Object Heap!).
18. Summer School 2012
CLR Object Lifecycle
Allocation (IL newobj)
Construction (.ctor)
Member usage (your code)
Cleanup (finalizer)
Deallocation (GC)
19. Object construction algorithm
Calculate the number of bytes required.
Append two system fields of type int.
Type pointer SyncBlockIndex
Make sure there is enough free storage space available in the managed heap.
Fill the block with zero bytes, call the constructor (pass NextObjPtr as this).
Increase NextObjPtr and return the address of newly created object.
20. Summer School 2012
Garbage Collection Triggers
1 • System is running low on memory
2 • A threshold of acceptable memory usage
has been exceeded on the managed heap.
3 • GC.Collect() has been called
21. Summer School 2012
Garbage Collection Algorithm
Suspend all threads except the one that triggered GC
Thread 1 [GC’ing] Thread 2 [suspended] Thread 3 [suspended]
Define application roots (live objects)
Stack roots GC handles Static objects CPU registers
Mark & Sweep
Heap Defragmentation (relocate objects).
23. Summer School 2012
Deterministic Cleanup: IDisposable
[ComVisibleAttribute(true)]
public interface IDisposable
{
void Dispose();
}
Dispose() — Performs application-defined tasks associated with freeing, releasing,
or resetting resources.
24. Summer School 2012
‘using’: syntactic sugar
using (Bitmap bitmap = new Bitmap(100, 100))
{
Console.WriteLine(bitmap.Height);
}
…is equivalent to:
Bitmap bitmap = null;
try
{
bitmap = new Bitmap(100, 100);
Console.WriteLine(bitmap.Height);
}
finally
{
if (bitmap != null)
{
(IDisposable)bitmap.Dispose();
}
}
25. Summer School 2012
Deterministic Cleanup: Finalization
Do NOT confuse with C++ destructor!!!
class UnmanagedResourceWrapper
{
// Declare and use an unmanaged resource...
~UnmanagedResourceWrapper()
{
// Clean up...
}
}
27. Summer School 2012
Some Best Practices
1 • DON’T use IDisposable and/or Finalize unless you really have to (see below).
• DON’T use Finalize if you do not have unmanaged resources (such as handles).
• Use IDisposable if your type works with managed resources or contains an
2 IDisposable member.
• Make sure you call base.Dispose() if base class is IDisposable.
3 • Use IDisposable AND Finalize if your type works with unmanaged resources.
• Finalize() should always release the resource and not throw any exceptions.
28. Summer School 2012
GCHandle Tricks
Lifetime monitoring & control
Pinning objects in memory
Weak references