![Page 1: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/1.jpg)
Garbage Collection in .Net
Wenjin Xu
![Page 2: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/2.jpg)
AgendaBasicsAllocationCollectionFinalizationGC ModeSample
![Page 3: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/3.jpg)
BasicIdea
Automatic memory managementTerms
Domain, Heap, Generation 0, Generation 1, Generation 2, Large object heap
![Page 4: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/4.jpg)
Heap
![Page 5: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/5.jpg)
AllocationInitial
CLR reserve memory for small object heap and large object heap(16MB / 64MB)
newobjAllocate memory from heap
![Page 6: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/6.jpg)
Generation Promotion
![Page 7: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/7.jpg)
CollocationWhen
Generation 0 is fullCall GC.Collect()Low memory
HowAssume all objects are garbageMark following object are reachable:Static fieldsMethod parametersLocal variablesCPU registersStrong ReferencesCollect
![Page 8: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/8.jpg)
Finalization
WhenGeneration 0 is fullCall GC.CollectLow memoryAppDomain unloadCLR is shutting down
![Page 9: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/9.jpg)
Dispose
![Page 10: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/10.jpg)
GC Mode
10
GC Mode
GC
Workstation Server
ConcurrentCollections
“Normal”Background GC
![Page 11: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/11.jpg)
GC Mode - DefaultSingle processor should use default Mode
![Page 12: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/12.jpg)
GC Mode – Concurrent GCIn background garbage collection, ephemeral generations (0 and 1) are collected as needed while the collection of generation 2 is in progress
![Page 13: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/13.jpg)
GC Mode – Server ModeHigh priority threadGC together, so garbage collection is fasterServer garbage collection often has larger size segments
![Page 14: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/14.jpg)
Pinned ObjectWhy do we need pinning
Interop with unmanaged codeHow objects get pinned
GCHandleType.PinnedAllowed by language syntax, eg. fixed in c#Args get pinned by the interop frame
Fragmentation problem
![Page 15: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/15.jpg)
Domain MonitoringCan not disable after enableGlobalPerformance hunt (about <1%)Does not count memory from Marshal.AllocHGlobal
![Page 16: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/16.jpg)
Memory LeakStrong ReferenceAllocate Memory by Marshal
![Page 17: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/17.jpg)
Sample查看性能计数器使用对象池节省新建对象造成的 GC压力使用 VSTS检查内存分配情况WinDBG + sosDebug vs Release
![Page 18: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/18.jpg)
ResourcePerformance Counter
http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx
BookCLR via C# 3rd Edition
Source Codehttp://referencesource.microsoft.com/netframework.aspx
![Page 19: Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample](https://reader035.vdocument.in/reader035/viewer/2022081421/551b5cc2550346dd1a8b6519/html5/thumbnails/19.jpg)
Thank you