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

Preview:

DESCRIPTION

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

Citation preview

Resources& Memory

Yuriy GutsR&D Engineer

yuriy.guts@eleks.com

Summer School 2012

.NET uses managed storage…So why do I need do know this

stuff?

Summer School 2012

Process memory layout (typical)

Summer School 2012

Storage management is hard

Summer School 2012

Manual• Fast• Precise• Deterministic

BUT

• Error-prone• Distracting• Hard to debug

Summer School 2012

Automaticvs.• Easy to program• Keeps you focused• Heuristically

optimized

BUT

• Unpredictable• Causes noticeable

delays• Sometimes leaks as

well

Common GC strategies

Summer School 2012

Mark & Sweep

Stop & Copy

Reference Counting

Mark & 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 2012

Stop & 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

Summer School 2012

• Remember the number of references to each object• Adjust it accordingly on every assignment

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

Summer School 2012

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

Object A Object B Object C Object D Object E Object F Managed heap

NextObjPtr

CLR Object Lifecycle

Summer School 2012

Allocation (IL newobj)

Construction (.ctor)

Member usage (your code)

Cleanup (finalizer)

Deallocation (GC)

Increase NextObjPtr and return the address of newly created object.

Fill the block with zero bytes, call the constructor (pass NextObjPtr as this).

Make sure there is enough free storage space available in the managed heap.

Append two system fields of type int.Type pointer SyncBlockIndex

Calculate the number of bytes required.

Object construction algorithm

Garbage Collection Triggers

Summer School 2012

• System is running low on memory1• A threshold of acceptable memory

usage has been exceeded on the managed heap.2

• GC.Collect() has been called3

Garbage Collection Algorithm

Summer School 2012

Heap Defragmentation (relocate objects).

Mark & Sweep

Define application roots (live objects)Stack roots GC handles Static

objectsCPU

registers

Suspend all threads except the one that triggered GCThread 1 [GC’ing] Thread 2

[suspended]Thread 3

[suspended]

GC Generations

Summer School 2012

Deterministic Cleanup: IDisposable

Summer School 2012

[ComVisibleAttribute(true)]public interface IDisposable{ void Dispose();}

Dispose() — Performs application-defined tasks associated with freeing, releasing, or resetting resources.

‘using’: syntactic sugar

Summer School 2012

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

Deterministic Cleanup: Finalization

Summer School 2012

Do NOT confuse with C++ destructor!!!

class UnmanagedResourceWrapper{ // Declare and use an unmanaged resource...

~UnmanagedResourceWrapper() { // Clean up... }}

Summer School 2012

1 2

3 4

Some Best Practices

Summer School 2012

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

1

• Use IDisposable if your type works with managed resources or contains an IDisposable member.

• Make sure you call base.Dispose() if base class is IDisposable.2• Use IDisposable AND Finalize if your type works with

unmanaged resources.• Finalize() should always release the resource and not throw any

exceptions.3

GCHandle Tricks

Summer School 2012

Lifetime monitoring & control

Pinning objects in memory

Weak references

Q & A

Summer School 2012

???yuriy.guts@eleks.com

Thank you!

Summer School 2012