Garbage Collection Patric Boscolo patboscmicrosoft com Garbage Collection
Garbage Collection Patric Boscolo patbosc@microsoft. com
Garbage Collection in. net http: //blogs. msdn. com/patricb Patric Boscolo Developer Evangelist Microsoft Deutschland Gmb. H patbosc@microsoft. com @patricsmsdn
C: WindowsSystem 32Agenda. exe Microsoft Windows [Version 7. 1. 7000] Copyright (c) 2008 Microsoft Corporation. All rights reserved. C: UsersUser. Name>dir mscoree. dll clr. dll GC. pptx Readme. txt Warning 4 3 2 1
Quick Reminder My Application Target WCF Win. Forms Direct. X ASP. net MVC Compiler Iron csc. exe, vbc. exe Python C# XNA Entity Framework WPF Work. Flow Visual Basic ASP. net Share. Point and a lot more. Assembly Visual F# Visual Process can host multiple versions of the CLR side by side Meta CLR A. net 4 *. exe, *. dll MSIL Process PE File CLR v 4 CLR v 2
Assembly *. exe, *. dll, *. sys, . . . The CLR. dll (Formaly known as mscorwks. dll) JIT (Formaly known as mscorjit. dll) ngen. exe BCL Base Class Library GC Profiling and Debugging APIs Loader and Binder Exception Handling Security Model MSIL Meta
The CLR evolved SP 1 3. 5 3. 0. NET 1. 0 . NET 1. 1 . NET 2. 0 . NET 4. 5 CLR 1. 0 CLR 1. 1 CLR 2. 0 CLR 4. 5 2002 2003 2005 - 2008 2010 2011 -x t
Value vs. Reference Types Object System. Object Value. Types Class Interface System. SByte System. Int 16 System. UInt 16 System. Single Array System. Byte byte int System. Int 32 uint System. UInt 32 double char long ulong System. Array String Sbyte System. Char short Sysem. Int 64 ushort System. UInt 64 float System. Double Enum System. String Delegate decimal System. Decimal boolean Others System. Boolean Structure Others Big. Integer System. Numerics Complex System. Numerics CLS - Compliant
Don‘t care about the Value Types, but remember the call by. Ref and call by. Value thing. VALUE TYPES DEMO
SOH JIT Stack obj 2 obj 1 Root Reference Static Next Object Pointer obj 1 0 x 000000 obj 2 0 x 000001 Memory Block Child Reference obj 3 0 x 000002 0 x 000003 obj 4 0 x 000004 0 xn Command Window new obj 1 = new obj 1(); new obj 2 = new obj 2{ obj = new obj 3()}; static obj 4 = obj. Get. Instance();
• • • Contigous Heap ˃ ˃ Objekte werden fortlaufend allokiert (Stack Prinzip) Dies geschieht via „next object pointer“ der zur Verfügung gestellt wird ˃ ˃ ˃ Stack Globals Statics CPU Registers Other Objects Allocation of objects < 85 k Objektreferenzen werden gehalten von X<85 k • Nicht mehr gebrauchte Objekte werden vom Garbage Collector „zerstört“ und der Speicher wird wieder zur Verfügung gestellt. SMALL OBJECT HEAP (SOH)
SOH JIT Stack obj 2 obj 1 Static Root Reference Next Object obj 1 0 x 000000 0 x 000001 Memory Block Child Reference obj 2 obj 3 0 x 000002 0 x 000003 Command Window obj 1 = null; obj 2 = null; GC. Collect(); obj 4 0 x 000004 0 xn
object = null; Don‘t set objects to null, since referencing causing a bigger memory footprint. Use just the scope and let the GC do the rest!
obj 1 Root Reference Child References obj 1
Good approach for UI and Caching WEAK - REFERENCES
Quelle: Garbage. First - Garbage Collection Paper Sun Microsystems http: //labs. oracle. com/jtech/pubs/04 -g 1 -paper-ismm. pdf
Generational Garbage Collector
Neueste Objekte sterben in der Regel schneller als ältere Ältere Objekte bleiben in der Regel am leben GC gruppiert Objekte in Generationen Short Lived „Gen 0“ Medium „Gen 1“ Long Lived „Gen 2“ • • Ein Objekt startet immer in Generation 0 Wenn ein Objekt einen GC lauf überlebt wird es in die nächste Generation gesetzt. GC komprimiert Gen 0 Objekte am meisten Je öfter der GC läuft desto größer wird die Auswirkung auf die Performance GENERATIONAL GARBAGE COLLECTOR
Stack static global obj 1 obj A 0 x 000000 0 x 000001 obj 2 obj C obj B 0 x 000002 Gen 2 0 x 000003 0 x 000004 0 x 000005 0 x 000006 Gen 1 0 x 000007 0 x 000008 obj D 0 x 000009 Gen 0 obj E 0 x 000000 A
Gen 0 Garbage Collection Stack static global obj 1 obj A 0 x 000000 0 x 000001 obj B 0 x 000002 Gen 2 0 x 000003 0 x 000004 0 x 000005 obj 2 obj C 0 x 000006 Gen 1 0 x 000007 0 x 000008 obj D 0 x 000009 Gen 0 obj E 0 x 000000 A
Gen 1 Garbage Collection Stack static global Next Object Pointer obj A 0 x 000000 0 x 000001 obj B 0 x 000002 Gen 2 0 x 000003 0 x 000004 obj B obj C 0 x 000005 0 x 000006 Gen 1 0 x 000007 0 x 000008 0 x 000009 Gen 0 0 x 000000 A
Gen 2 Garbage Collection Stack static global Next Object Pointer obj A 0 x 000000 0 x 000001 obj B 0 x 000002 Gen 2 obj C 0 x 000003 0 x 000004 0 x 000005 0 x 000006 Gen 1 0 x 000007 0 x 000008 0 x 000009 Gen 0 0 x 000000 A
GC wird ausgeführt, wenn Objekte folgende Grenze erreicht haben: Gen 0 Objects reach ~256 K Gen 1 Objects reach ~2 Mb Gen 2 Objects reach ~10 Mb Oder System Memory gering ist Die meisten Objekte sollten in Gen 0 sterben Gen 2 Collection hat die meisten Performance Auswirkungen Der komplette SOH wird komprimiert Large Object Heap wird collected THRESHOLDS
DEMO GENERATIONS
When I‘m Good I‘m really Good Release When I‘m Bad I‘m Better!
Microsoft Visual Studio 2011 Developer Preview Debugging. cs File Edit View Build Debug Team Data Tools Debug Item 1. cs Item 2. cs Test Analyze Windows Help Any CPU Item 3. cs Toolbox //build Team Explorer text text WDK for Visual Studio 2011 Developer Preview WINDBG in Visual Studio Team Explorer Solution Explorer
Your Application 1200 1000 800 600 400 200 0 10 Gen 1 Objects Gen 2
Tools Perfmon http: //msdn. microsoft. com/en-us/library/x 2 tyfybc. aspx Visual Studio 2010 – Performance Tools http: //msdn. microsoft. com/en-us/library/dd 264934. aspx Red. Gate Ants Memory Profiler http: //www. red-gate. com/products/dotnet-development/ants-memory-profiler/ Jetbrains dot. Trace http: //www. jetbrains. com/profiler/ . . .
Temporäre Objekte • Einmal allokiert kann ein Objekt seine größe nicht mehr verändern • Objekte wie strings sind unveränderbar – Können nicht verändert werden, neue Objekte werden stattdessen erzeugt – Der Heap wird mit Temporären Objekten gefüllt – Der GC wird öfters ausgeführt C: WindowsSystem 32my. App. exe Hallo Welt, Hallo Universum Hallo Welt, Hallo Welt Hallo Microsoft Windows [Version 7. 1. 7000] Copyright (c) 2008 Microsoft Corporation. C: UsersUser. Name>my. App. exe String hello = “Hallo”; Hello += “ Welt, ”; Hello += “ Hallo”; Hello += “ Universum”; Console. Write. Line(hello); >Hallo Welt, Hallo Universum All rights reserved.
Generational Garbage Collector Ephemeral Generations Gen 0 GC collected nur Ojekte der Gen 0 Partition ( Objekte mit kurzer Lebensdauer) Neue Objekte werden in Gen 0 allokiert es sei denn, eshandelt sich um sehr große Objekte, dann werden Sie direkt im LOH als Gen 2 allokiert. Die meisten temporären Objekte werden in Generation 0 allokiert und überleben keine Gen 0 GC. Gen 1 GC collected Objekte der Gen 0 + 1 (Objekte mit kurzer Lebensdauer) Gen 2 GC collected Objekte der Gen 0 + 1 +2 (auch Objekte mit langer Lebensdauer) Survivor (Objekte die einen GC überlebt haben werden in die nächste Generation promoted)
• • Viele Objekte nutzen folgende Dienste ˃ Disk ˃ Network ˃ UI Resources ˃ Interop / Native Resourcen Diese Dienste benötigen „safe cleanup“ nachdem Sie von. net Klassen verwendet worden sind. • Object Finalization garantiert das Code zum aufräumen ausgeführt wird, bevor der Garbage Collector ausgeführt wird. • Finalizable Objects überleben mindestens 1 extra GC Durchgang und sind oft Objekte der Generation 2 • Finalizable Klassen haben ˃ Finalize Method (C# or VB. net) ˃ C++ style Destructor (C#) EVIL FINALIZER
Evil Finalizer (SOH) Finalization Queue f. Reachable Queue Stack static Next Object Pointer obj A 0 x 000001 obj D obj B 0 x 000002 Gen 1 0 x 000003 obj C 0 x 000004 obj D 0 x 000005 0 x 000006 Gen 0 0 x 000007 0 x 000008 0 x 000009 0 x 000000 A
Evil Finalizer (SOH) Finalization Queue f. Reachable Queue Stack static Next Object Pointer obj A 0 x 000001 Finalizer Thread obj D 0 x 000002 Gen 1 0 x 000003 0 x 000004 0 x 000005 0 x 000006 Gen 0 0 x 000007 0 x 000008 0 x 000009 0 x 000000 A
EVIL FINALIZER DEMO
Microsoft Visual Studio Item 1. cs File Edit View Build Debug Team Data Tools Debug Item 1. cs Item 2. cs Test Analyze Windows Help Any CPU Item 3. cs Toolbox namespace evilfinalizer { public class Test : IDisposable { public void Dispose() { GC. Suppress. Finalize(this); Team Explorer text Clean. Up(true); } private void Clean. Up(bool code. Dispose) { if(code. Dispose) { //Dispose called in code not by GC } // Perform resource cleanup here } text public void Finalize() { Clean. Up(false); } ~Test() { Clean. Up(false); } } } Team Explorer Solution Explorer
• • • Allokiert Objekte >= 85 K Nicht Komprimierter Heap Objekte werden via „Free Space Table“ allokiert GC startet wenn LOH Grenzen erreicht sind Benutzt eine „Free Space Table“ um Adressen im Speicher zu finden wo Objekte allokiert werden können, anstelle eines „Next Objects Pointer“. X>85 k LARGE OBJECT HEAP (LOH)
Large Object Heap Stack obj static obj A 0 x 000001 obj C obj B 0 x. FF 94208 0 x. FF 182272 Large Memory Block Free Space Table 0 x. FF 42500 0 x. FF 16777216 From To FF 42500 FF 16777216
Large Object Heap Stack obj static obj A 0 x 000001 obj C obj B 0 x. FF 94208 0 x. FF 182272 Large Memory Block Free Space Table 0 x. FF 42500 0 x. FF 16777216 From To FF 42500 FF 16777216 FF 94208 FF 182272
Large Object Heap Stack obj static obj A 0 x 000001 obj C 0 x. FF 94208 0 x. FF 182272 Large Memory Block Free Space Table obj D 0 x. FF 42500 0 x. FF 16777216 From To FF 42500 FF 94208 FF 16777216 FF 182272 FF 94208 FF 182272
Microsoft Visual Studio Item 1. cs File Edit View Build Debug Team Data Tools Debug Item 1. cs Item 2. cs Test Analyze Windows Help Any CPU Item 3. cs Toolbox namespace bigloader { public class Bootstrap { // some more things private XDocument settings = new XDocument(); public Bootstrap() { // some load stuff // some more load stuff settings != null; settings. Load(@”c: /path. . . ”); //some more stuff } Team Explorer text text } } Team Explorer Solution Explorer
CLR 2 Thread 1 Thread 2 Thread 3 Allocating GC Allocating Suspended Allocating Heaps SOH LOH Client GC one Server GC one per Logical Processor Gen 0/1 Gen 2 Client GC always blocking can be non-blocking Server GC always blocking can be non-blocking Collection Flavours
CLR Thread 1 Thread 2 Thread 3 GC Thread 1 GC Thread 2 Allocating Suspended Allocating Waiting GC Waiting
CLR 4 Client + CLR 4. 5 Server Background (Async) GC replaces Concurrent GC Init Thread 1 Thread 2 Thread 3 GC Thread 1 GC Thread 2 BGC Thread 1 BGC Thread 2 Waiting GC 0/1 GC 2
Garbage Collector Notifications in. net 4. 0 • Disable Concurrent GC <configuration> <runtime> <gc. Concurrent enabled="false"/> </runtime> </configuration> • Register. For. Full. GCNotification – Registers for: • • Wait. For. Full. GC Approach Wait. For. Full. GCComplete Client Mode
Was gibts neues in GC Iteration 4 http: //msdn. microsoft. com/de-de/library/0 xy 59 wtx. aspx
Summary 4 3 2 1
http: //blogs. msdn. com/patricb
FRAGEN?
Ihr Feedback ist uns wichtig
Vielen Dank!
- Slides: 53