Intro● Stack & heap● Heap fragmentation – External – Internal● Approaches to memory management – C — malloc / free – C++ — new / delete, smart pointers – Python — GC with reference counting – .NET CLR — GC with mark & sweep
Value types & reference types● Value type – Primitive built-in (int, bool, ..), enums & user -defined structures – Usually lives on stack – Assigned & compared by value● Reference type – Allocated on heap – Assigned & compared by reference – Subject to GC● Boxing / unboxing
Heaps in .NET● SOH (Small Object Heap) – 3 Generations – Mark & sweep – Compacting● LOH (Large Object Heap) – Holds objects >85K – Free space table – Not compacted
Heap Allocation● SOH – Allocation is in Gen0 heap – Consecutive allocation; Next Object Pointer (NOP) – Fast – No internal fragmentation● LOH – Free Space table lookup – Internal fragmentation● Heap expansion – For LOH, heap expansion is preferred to full GC
Garbage collection : Mark & Sweep● Mark phase – GC roots ● Stack ● Statics ● CPU registers ● fReachable – Reachability queue (example) – Islands of isolation
Garbage collection : Mark & Sweep● Sweep phase – SOH — with compaction – LOH — no compaction – Fragmentation considerations
Gen0 Collection● Most frequest● Least expensive● Objects in Gen0 can – Die – Be copied to Gen1● Always leaves Gen0 empty
Gen1 Collection● Collects Gen0 and Gen1 heaps● More expensive than Gen0 collection● Objects can: – Die – Be copied from Gen0 to Gen1 – Be copied from Gen1 to Gen2
Gen2 (Full) Collection● Collects ALL heaps including LOH● Most expensive GC operation● Objects can: – Die – Be copied from Gen0 to Gen1 – Be copied from Gen1 to Gen2 – Stay in Gen2 – Be freed from LOH – Stay in LOH
Finalization● Allows to guarantee unmanaged resource cleanup● Finalize() and ~ClassName()● Finalizer rules – No exceptions – Unmanaged resources only – No use of other finalizable objects
Links & Books● Jeffrey Richter, CLR via C#● Notes on the CLR garbage collector http://www.vineetgupta.com/2007/01/notes-on-the-c● Under the Hood of .NET Memory Management ftp://support.red-gate.com/ebooks/under-the-hood-o● Tesss blog http://blogs.msdn.com/b/tess/archive/2008/04/17/ho●
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.