inside the garbage collector - sdd...
TRANSCRIPT
![Page 1: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/1.jpg)
InsidetheGarbageCollector
![Page 2: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/2.jpg)
2
Agenda
• ApplicationsandResources• TheManagedHeap• MarkandCompact• Generations• Non-MemoryResources• Finalization• HinderingtheGC• HelpingtheGC
![Page 3: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/3.jpg)
3
ApplicationsandResources
• Applicationsusemanydifferentkindsofresourceswhenexecuting– Memory– Filehandles– Databaseconnections– Sockets
• .NETprovidesinfrastructureandpatternsformanaginganapplicationsresources
![Page 4: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/4.jpg)
4
MemoryandtheManagedHeap
• .NEThasspecificfunctionalitytoautomatememorymanagement– Applicationsrequestmemoryusingnewkeyword– MemoryisallocatedinareacalledtheManagedHeap
• RuntimecleansupmemorywhennecessaryusingGarbageCollection (GC)– Onlymanagedheapisgarbagecollected.Unmanagedandstack
allocatedmemoryunaffected– GarbageCollectorremovesobjectsnolongerinuse
![Page 5: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/5.jpg)
5
HowtheGarbageCollectorWorks
• Applicationsrunasifmemoryisinfinite• GCisinvokedwhenresourcethresholdbreached• Assumesallobjectsaregarbage• Collectsthosenotinuse
A B C D E F B D E G
![Page 6: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/6.jpg)
6
WhatObjectsareinUse?
• Thegarbagecollectordoesnotcollectobjectsinuse– Howdoesitknowwhatisinuseandwhatisnot?
• GChasconceptofobjectsbeingreachable– Objectsthatcouldbeaccessedfromcodethatisyettorun
• GCwalksobjectsgraphunderliveroots– Non-nullstaticreferences– Localvariablesonthestackthatarestillinuse– Afewmoreexotickinds,e.g.thenativecodeinterop infrastructure
• AnyobjectreachablefromaliverootsurvivestheGC
![Page 7: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/7.jpg)
7
SpottheLiveRoots
• WhatreferencesareliverootswhentheGCruns?
static object o1 = new object();
void RunTest(){object o2 = new object();object o3 = new object();
GC.Collect();
o1 = null;
GC.Collect();
ProcessObject(o3);
GC.Collect();}
Whatisgarbagehere?
Whatisgarbagehere?
Whatisgarbagehere?
![Page 8: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/8.jpg)
8
GCOptimization- Generations
• ExaminingtheentireheapeveryGCtooexpensive– Optimization:ifobjectsurvivedlastGCthenprobablystillalive– Bydefaultonlychecksthemostrecentlyallocatedobjects
• .NETGCisGenerational– Threegenerations– Allobjectsallocatedintogen0– Anyobjectsurvivingacollectionispromotedtonextgeneration
A B C D E F B D E G
Gen0 Gen1 Gen0
GC
![Page 9: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/9.jpg)
9
TheImplicationsofGenerations
• RelativecostofGC– Gen0isverycheaptocollect– Gen1ismoreexpensivetocollectthatgen0– Gen2canbeveryexpensivetocollect
• HealthyGC– Orderofmagnitudebetweennumberofcollectionsofeachsuccessive
generationregardedashealthy– gen0:gen1:gen2=>100:10:1
• Generallydon’ttrytosecondguesstheGC
![Page 10: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/10.jpg)
10
GCOptimization– GCStyles
• ThreestylesofGC– Non-concurrent– Concurrent– Asynchronous
![Page 11: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/11.jpg)
11
ConcurrentandNon-Concurrent
• Non-concurrent– GCstartsandrunsthroughtocompletionwithoutinterruption– AlwaysusedforGen0andGen1collects
• Concurrent– Applicationthreadsinterleavedwithcollection– Keepsapplicationresponsive– OnlyusedforsomeGen2collects– CancontinueapplicationexecutionuntilanotherGCrequired
![Page 12: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/12.jpg)
12
Async
• Asynchronous– Introducedinversion4.0– Similartoconcurrentbutcanperformgen0andgen1collectsduringa
gen2collect
![Page 13: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/13.jpg)
13
ConcurrentGC
Gen2GCThread
ApplicationThread
ThreadSuspended
new()
new()
new()
Allocation requires Gen0 collect
Gen 2 collection complete
![Page 14: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/14.jpg)
14
BackgroundGC
Gen2GCThread
ApplicationThread
ThreadSuspended
new()
new()
new()
Allocation requires Gen0 collect
Gen 2 collection complete
![Page 15: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/15.jpg)
15
GCModes
• TherearethreeGCmodes– WorkstationConcurrent(default)– WorkstationNon-Concurrent– Server
• Controlledbyconfig flags<configuration>
<runtime><gcServer enabled="true"/><gcConcurrent enabled="false"/>
</runtime></configuration>
![Page 16: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/16.jpg)
16
WorkstationConcurrent
• DesignedtokeepUIapplicationsinteractive– UIthreadpausedasshortatimeaspossible
• Gen0andGen1usenon-concurrentGC– UIthreadpausetimewouldbeinsignificant
• Gen2canuseconcurrent– Gen2canbeexpensive– KeepsUIthreadpumpingthemessagequeue
• Gen2canuseasynchronous– MeansUIthreadstaysfree-runningevenifanotherGCisrequired– Onlyavailableon.NET4.0andlater
![Page 17: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/17.jpg)
17
Workstationnon-concurrent
• WholeGCisnon-concurrent– Optimizedfornon-UIcode– GCkidnapsallocatingthread– FewerCPUcyclesthanworkstationconcurrent
![Page 18: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/18.jpg)
18
ServerGC
• Tunedformultipleprocessors– Eachprocessorgetsownblockofmanagedheapforallocation/GC
calledarena– Allowslockfreeallocationacrossmultiplecores
• Alwaysnon-concurrent– GCtakesplaceonallprocessorsatthesametime
![Page 19: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/19.jpg)
19
GCOptimization– SelfTuning
• GCbehavior dependentonyourapplication– Usesheuristicstoworkoutefficientstrategy– IfGCofagenerationiseffectivethenperformsmore– IfGCofagenerationreclaimslittlememorythenperformsless
• GCselftuningnormallyproducesoptimalresults– Beststrategyforyourapplication
• AvoidGC.Collect– DisruptsGCstatisticsandsoselftuninglesseffective
![Page 20: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/20.jpg)
20
LargeObjects
• Movinglargeobjectstooexpensive– Largeobjectover85000bytes
• AllocatedinLargeObjectHeap
![Page 21: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/21.jpg)
21
MemoryisnottheonlyResource
• Onlymemorymanagementisautomated– Runtimedoesnotknowwhenuseofotherresourcesiscomplete
• Typesthatcontrolnon-memoryresourcesfollowstandardpattern– ImplementIDisposable– ConsumercallsDisposewhentheyarefinishedpublic interface IDisposable{
void Dispose();}
![Page 22: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/22.jpg)
22
WhenShouldYouImplementIDisposable?
• MostclassesdonotneedtoimplementIDisposable– Donotconsumeresourcesotherthanmemory
• ClassesthatdirectlycontrolresourcesacquiredviainteropmustimplementIDisposable– Noothermanagedtypecanreleasethoseresources
• ClassesthataggregateothertypesthatimplementIDisposable shouldimplementIDisposable themselves– ProvidesahooktoDisposeaggregatedobjects
![Page 23: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/23.jpg)
23
WhatifIdon’tCallDispose?
• Onlyanissuefortypesthatdirectlycontrolunmanagedresources– FileStream– SqlConnection– Icon
• Needlastchancemechanismforfreeingupresources– Nooneelsecanfreeupresources
• RuntimehasconceptofFinalization– Veryunlikelythatyourclasseswillneedafinalizer
![Page 24: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/24.jpg)
24
HowFinalizationWorks
• TypesthatsupportfinalizationoverrideFinalizevirtualmethodonSystem.Object– C#syntaxlookslikeaC++destructor
• Runtimeseestypehasfinalizer wheninstantiated– Putsobjectinfinalizationqueue
class SharedMemory{
~SharedMemory(){}
}
![Page 25: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/25.jpg)
25
FinalizationandGC
• WhenGCdecidestocollectobjectitseesit’sfinalizable– Movestheobjectontofreachable queue– freachable queueactsasliveroot(finalizationreachable)
• Separatefinalizationthreadprocessesfreachable queue– Callsfinalizer andremovesobjectfromqueue– ObjectcanbecollectednexttimeGClooksatit
![Page 26: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/26.jpg)
26
ConsequencesofFinalization
• Finalizable objectswillalwayssurviveoneGC– Alwayspromotedtogen1orgen2– Consequentlymoreexpensivetocollect
• Timingoffinalizer executionnon-deterministic– AfterGCnoticesitisgarbage– WhenFinalizationthreadgetsroundtoit
• Limitedfunctionalityinfinalizer– Shouldnotcallotherobjectsasmayalreadyhavebeenfinalized
• Finalizable classesshouldalwaysimplementIDisposable– Cansuppressfinalization inDispose
public void Dispose(){
GC.SuppressFinalize(this);}
![Page 27: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/27.jpg)
27
HinderingandHelpingtheGC
• ThreemainissuesforGC– Unnecessarypromotion– Referenceheavydatastructures– Pinning
![Page 28: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/28.jpg)
28
UnnecessaryPromotion
• WanttominimiseGen2collections– IdeallyallGen2objectsliveforever– Notrealistic
• Promotethendieisworstscenario– Badlytunedcaches– Finalizers– “ad-hoc”caching– Notreleasingdeadobjects
![Page 29: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/29.jpg)
29
UnnecessaryPromotion
• Tunecachetimeouts– DropcacheinGen1– Makeitlonglivedtomakethehitworthwhile
• Removeunnecessaryfinalizers– IfabsolutelyrequiredrefactorcodeintoSafeHandle
public abstract class SafeHandle : CriticalFinalizerObject,IDisposable
{...
}
![Page 30: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/30.jpg)
30
“Ad-hoc”Caching
• ConsiderWeakReference– ObjectaccessibleunlessGC’d
“ProbablyneedthisobjectagainsoonsoI’llhangontoit”
WeakReference wr = new WeakReference(new object());
object temp = wr.Target;if(temp == null){
Console.WriteLine("Collected");}else{
Console.WriteLine("Still alive");}
![Page 31: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/31.jpg)
31
HangingontoDeadObjects
• Setmemberandstaticreferencestonullwhendone– GCcancleanupassoonaspossible
• Especiallyimportantwhenreplacingobject– Becarefulofthreadingissues
class ReferenceData{
XElement data;
public void ReloadData(string filename){
data = null;data = XElement.Load(filename);
}}
![Page 32: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/32.jpg)
32
ReferenceHeavyStructures
• Markphasemadeharderbymanyreferences– GChastochasedownembeddedobjectgraphs
• Thinkofrevisedoroptimisedwaysofrepresentingdata– Arrays– AdjacencyMatrices
• Example:ThreadPoolQueuebefore4.0
![Page 33: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/33.jpg)
33
ThreadPoolQueueBefore4.0
• Linkedlistofworkitems– Rootisnevergarbage– Lotsofwork=lotsofreferences
Node WorkItem
Node WorkItem
Node WorkItem
![Page 34: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/34.jpg)
34
ThreadPoolQueueAfter4.0
• Linkedlistofarraysofworkitems– Lotsofwork=fewreferences
WorkItem
WorkItem
WorkItem
WorkItem
WorkItem
WorkItem
![Page 35: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/35.jpg)
35
Pinning
• PinningmeansGCcan’tmoveobject– Requiredininterop scenarios
• ConsiderinflatinglargebufferstobeallocatedinLOH– PinningirrelevantinLOH– ReuseLOHbuffers
![Page 36: Inside the Garbage Collector - SDD Conferencesddconf.com/brands/sdd/library/Playing_nicely_with_the... · 2017. 5. 11. · • The garbage collector does not collect objects in use](https://reader033.vdocuments.us/reader033/viewer/2022051912/60038415eea73310111f6631/html5/thumbnails/36.jpg)
36
Summary
• GCautomatesmemoryreclamation• GCisheavilyoptimized• ReleasemodeGCmuchmoreaggressivethandebug• NonmemoryresourcesfreedupusingIDisposable pattern• Finalizationexistsasalastresortforframeworkclasses.You
shouldnotneeditinyourcode• YoucanhelptheGC• YoucanhindertheGC