net [core] internals garbage collection...2019/01/17 · garbage collection steps 1. trigger 2....
TRANSCRIPT
![Page 1: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/1.jpg)
.NET [Core] InternalsGarbage Collection
Robert Hakensoftware & cloud architect, HAVIT, [email protected], @RobertHaken, https://knowledge-base.havit.cz + .euMicrosoft MVP: Development, MCT, MCPD: Web, MCSE: Cloud
![Page 2: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/2.jpg)
AGENDA
Architektura paměti .NET aplikací
Managed Heap a Garbage Collection
Roots
Generations
Large Object Heap
Finalization, IDisposable
Další optimalizace GC
![Page 3: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/3.jpg)
DEMO
Heap Inspection (02-ManagedHeap)
![Page 4: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/4.jpg)
Architektura paměti .NET aplikací
Instrukční paměť
Zásobník (Stack)
Halda (Heap)
–Managed Heap (.NET, Garbage Collector)
–Native Heap (Unmanaged Resources)
![Page 5: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/5.jpg)
Halda (ManagedHeap)
Garbage Collector ~ 1959 LISP
Garbage Collection–překročení thresholdugenerace při alokaci
–system-wide memory pressure
–GC.Collect() API (never-ever!)
bump pointer allocation vs. free-list
sesypání, update referencí
![Page 6: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/6.jpg)
![Page 7: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/7.jpg)
DEMO
Garbage Collection (03-SimpleGarbageCollection)
![Page 8: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/8.jpg)
Roots
kořeny grafu objektů pro zjištění dosažitelnosti
– zásobník (lokální proměnné, parametry metod)
– GCHandles
• globální statické fieldy
• pinnedobjects
• ...
– F-reachablequeue
(WeakReference)
![Page 9: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/9.jpg)
DEMO
GC Roots (04-GCRoots)
![Page 10: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/10.jpg)
Generations
výkonová optimalizace
soustředí se objekty s krátkou životností
Gen 0 – nové, Gen 1 & 2 – přežily 1/více-krát
Gen 0 + 1 = ephemeral segment („fixed“ size)
Gen 2 = variablesize
![Page 11: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/11.jpg)
Generations
![Page 12: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/12.jpg)
DEMO
Generations (05-Generations)
![Page 13: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/13.jpg)
Finalization
explicitní úklid unmanaged zdrojů
Finalize() ~ C# destructor
Finalization Queue=> do Gen 1
F-reachableQueue
Finalization Thread
IDisposable, ResourceWrapper pattern
![Page 14: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/14.jpg)
DEMO
![Page 15: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/15.jpg)
LargeObjectHeap
výkonová optimalizace
objekty větší než 85 000 bytů (default)
součást Gen 2 collection
nesetřásá se (NET 4.5.1+ lze jednorázově)
Free List
![Page 16: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/16.jpg)
DEMO
Large Object Heap (06-LargeObjectHeap)
![Page 17: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/17.jpg)
Garbage Collection Steps
1. Trigger
2. Suspend managed threads (vs. concurrent GC)
3. User-thread runs GC code
4. Select generation
5. Mark (GCRoots, Remembered Sets vs. Card Tables)
6. Plan (Compact/Sweep)
7. Sweep / Compact + Relocate
8. Resume managed threads
![Page 18: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/18.jpg)
Verze GarbageCollectoru
Workstation (lag) vs. server (throughput)configuration/runtime/gcServerenabled="true|false"
Background GC (NET4 wksonly, NET4.5 svr)
configuration/runtime/gcConcurrentenabled="true|false"
Objekt >2GB (x64, NET4.5)
LOH Free ListsOptimization (NET4.5)
LOH HeapBalancing (NET4.5)
LOH Compaction, explicit (NET4.5.1)
![Page 19: NET [Core] Internals Garbage Collection...2019/01/17 · Garbage Collection Steps 1. Trigger 2. Suspend managed threads (vs. concurrent GC) 3. User-thread runs GC code 4. Select](https://reader036.vdocuments.mx/reader036/viewer/2022071106/5fe0da5ecb148a1bc8221ff3/html5/thumbnails/19.jpg)
Q & ARobert Haken
MVP ASP.NET/IIS, MCT
@RobertHaken, [email protected]
http://knowledge-base.havit.cz