30
Resources & Memory Yuriy Guts R&D Engineer [email protected] Summer School 2012

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

Resources& Memory

Yuriy GutsR&D Engineer

[email protected]

Summer School 2012

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

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

stuff?

Summer School 2012

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

Process memory layout (typical)

Summer School 2012

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

Storage management is hard

Summer School 2012

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

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

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

Common GC strategies

Summer School 2012

Mark & Sweep

Stop & Copy

Reference Counting

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

Mark & Sweep

Summer School 2012

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

Advantages

• Keeps objects in their places• Will work well with pointer arithmetics

Summer School 2012

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

Disadvantages

• Can cause heap fragmentation• Must process every object in the heap• May require O(n) space to execute

Summer School 2012

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

Stop & Copy

Summer School 2012

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

Advantages

• Fast allocation and collection• Operates only on live objects• Does not cause fragmentation

Summer School 2012

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

Disadvantages

• May require some time and space to rearrange pointers on live objects

• Unsuitable for C / C++

Summer School 2012

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

Reference Counting

Summer School 2012

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

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

Advantages

• Easy to implement• Collects garbage incrementally without

significant delays

Summer School 2012

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

Disadvantages

• Unable to clean up circular structures• Requires atomic operations (expensive)• Slows down assignment operations

Summer School 2012

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

Which approach does .NET use?

Summer School 2012

???

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

.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

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

CLR Object Lifecycle

Summer School 2012

Allocation (IL newobj)

Construction (.ctor)

Member usage (your code)

Cleanup (finalizer)

Deallocation (GC)

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

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

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

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

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

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]

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

GC Generations

Summer School 2012

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

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.

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

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

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

Deterministic Cleanup: Finalization

Summer School 2012

Do NOT confuse with C++ destructor!!!

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

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

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

Summer School 2012

1 2

3 4

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

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

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

GCHandle Tricks

Summer School 2012

Lifetime monitoring & control

Pinning objects in memory

Weak references

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

Q & A

Summer School 2012

[email protected]

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

Thank you!

Summer School 2012