36
Inside the Garbage Collector

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

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

36

Summary

• GCautomatesmemoryreclamation• GCisheavilyoptimized• ReleasemodeGCmuchmoreaggressivethandebug• NonmemoryresourcesfreedupusingIDisposable pattern• Finalizationexistsasalastresortforframeworkclasses.You

shouldnotneeditinyourcode• YoucanhelptheGC• YoucanhindertheGC