ELEKS Summer School 2012: .NET 04 - Resources and Memory
Upcoming SlideShare
Loading in...5
×
 

ELEKS Summer School 2012: .NET 04 - Resources and Memory

on

  • 266 views

"Resources and Memory" lecture @ ELEKS Summer School 2012 (Jul 2012)

"Resources and Memory" lecture @ ELEKS Summer School 2012 (Jul 2012)

Statistics

Views

Total Views
266
Views on SlideShare
266
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

ELEKS Summer School 2012: .NET 04 - Resources and Memory ELEKS Summer School 2012: .NET 04 - Resources and Memory Presentation Transcript

  • Summer School 2012Resources& Memory Yuriy Guts R&D Engineer yuriy.guts@eleks.com
  • Summer School 2012 .NET uses managed storage…So why do I need do know this stuff?
  • Summer School 2012Process memory layout (typical)
  • Summer School 2012Storage management is hard
  • 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
  • Summer School 2012Common GC strategies Mark & Sweep Stop & Copy Reference Counting
  • Summer School 2012Mark & Sweep
  • Summer School 2012 Advantages• Keeps objects in their places• Will work well with pointer arithmetics
  • Summer School 2012 Disadvantages• Can cause heap fragmentation• Must process every object in the heap• May require O(n) space to execute
  • Summer School 2012Stop & Copy
  • Summer School 2012 Advantages• Fast allocation and collection• Operates only on live objects• Does not cause fragmentation
  • Summer School 2012 Disadvantages• May require some time and space to rearrange pointers on live objects• Unsuitable for C / C++
  • Summer School 2012 Reference Counting• Remember the number of references to each object• Adjust it accordingly on every assignment
  • Summer School 2012 Advantages• Easy to implement• Collects garbage incrementally without significant delays
  • Summer School 2012 Disadvantages• Unable to clean up circular structures• Requires atomic operations (expensive)• Slows down assignment operations
  • Summer School 2012 ?? ?Which approach does .NET use?
  • 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!).
  • Summer School 2012 CLR Object LifecycleAllocation (IL newobj) Construction (.ctor) Member usage (your code) Cleanup (finalizer) Deallocation (GC)
  • 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.
  • Summer School 2012Garbage Collection Triggers1 • System is running low on memory2 • A threshold of acceptable memory usage has been exceeded on the managed heap.3 • GC.Collect() has been called
  • 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).
  • Summer School 2012GC Generations
  • 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.
  • Summer School 2012 ‘using’: syntactic sugarusing (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(); }}
  • Summer School 2012 Deterministic Cleanup: FinalizationDo NOT confuse with C++ destructor!!!class UnmanagedResourceWrapper{ // Declare and use an unmanaged resource... ~UnmanagedResourceWrapper() { // Clean up... }}
  • Summer School 2012
  • Summer School 2012 Some Best Practices1 • 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 an2 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.
  • Summer School 2012 GCHandle TricksLifetime monitoring & controlPinning objects in memoryWeak references
  • Summer School 2012 ?? ? Q&Ayuriy.guts@eleks.com
  • Summer School 2012Thank you!