1.35k likes | 1.44k Views
Ga r b a g e Collection. M e m or y M a n a ge m en t S o F a r. S om e items are p r e a ll o c a t e d a nd p e r sist t h r ou g h ou t th e p r o g r a m:. ●. What are they? S om e are a l loc a te d o n th e r un tim e st a c k:. ●. ●. What are they? . ●.
E N D
MemoryManagementSo Far Someitemsarepreallocatedandpersistthroughouttheprogram: ● What are they? Someare allocatedontheruntimestack: ● ● What are they? ● Someare allocatedintheheap: ● What are they? ● Howdowemanageheap-allocatedmemory? ●
ManualMemoryManagement • OptionOne:Have programmerhandleheap allocationand deallocation C,C++ Advantages? Disadvantages? ● ●
AutomaticMemoryManagement Idea:Runtime environmentautomaticallyreclaimmemory. What is the garbage? Advantages? ● ● Disadvantages?
Can you Identify Garbage? Whatisgarbageattheindicatedpoints? ● int main() { Object x, y; x= new Object(); y= new Object(); /* Point A */ x.doSomething(); y.doSomething(); /* Point B */ y= new Object(); /* Point C */ }
ApproximatingGarbage Ingeneral,undecidablewhetheranobjectisgarbage. Needtorelyonaconservativeapproximation. Anobject isreachableifitcanstillbereferencedbytheprogram. Garbage Collector - Detectandreclaimunreachableobjects.
GC Assumptions Assumethat,atruntime,wecanfindall existingreferencesintheprogram. ● Cannotfabricateareferencetoanexistingobject Cannotcastpointerstointegersorvice-versa. ● ● Examples:Java,Python,JavaScript,PHP,etc. Non-examples:C,C++ ● ●
GC Types Incrementalvsstop-the-world: Incremental- runconcurrentlywithprogram Stop-the-world- pauseprogramexecutiontolookforgarbage Whichis(generally)moreprecise? Whichwould youuseinanuclearreactorcontrolsystem? CompactingvsNon-compacting: compacting- movesobjectsaroundinmemory. non-compacting- leavesallobjectswheretheyoriginated. Which(generally)spendsmoretimegarbagecollecting?Which(generally)leadstofasterprogramexecution?
Major Approaches to GC • Reference Counting • Mark and Sweep • Stop and Copy • Generational
ReferenceCounting Technique * Storeineachobjectareferencecount(refcount)trackinghowmanyreferencesexisttotheobject. * Createareferencetoanobject:incrementsrefcount. * Removeareferencetoanobject:decrementsrefcount. * Whenobjectrefcount==0,unreachable&reclaimed. Mightdecreaseotherobjects'refcountsandtriggermorereclamations. ●
ReferenceCountinginAction class LinkedList { LinkedList next; } int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; } int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; } head int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; } 0 head int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; } 0 head int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; } 1 head int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; } 1 head int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; } 1 head int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; mid head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { 1 head mid 0 head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { 1 head mid 0 head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { 1 head mid 1 head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { 1 head mid 1 head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { 1 head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { 1 head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 0 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { 1 head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 0 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 1 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 1 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 2 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 2 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 2 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 1 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 0 head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 0 head.next.next = null; Reclaimed! head = null; }
ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; 0 head Reclaimed! int main() { mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 0 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { head mid 0 tail Reclaimed! head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
ReferenceCountinginAction class LinkedList { LinkedList next; } head int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; mid tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }
Reference Counting:OneMajorProblem class LinkedList { LinkedList next; } int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; head.next = mid; mid.next = tail; tail.next = head; head = null; mid = null; tail = null; }
OneMajorProblem class LinkedList { LinkedList next; head } mid int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; tail head.next = mid; mid.next = tail; tail.next = head; 2 head = null; mid = null; tail = null; }
OneMajorProblem class LinkedList { LinkedList next; head } mid int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; tail head.next = mid; mid.next = tail; tail.next = head; 2 head = null; mid = null; tail = null; }
OneMajorProblem class LinkedList { LinkedList next; head } mid int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; tail head.next = mid; mid.next = tail; tail.next = head; 2 head = null; mid = null; tail = null; }
OneMajorProblem class LinkedList { LinkedList next; head } mid int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; tail head.next = mid; mid.next = tail; tail.next = head; 2 head = null; mid = null; tail = null; }
OneMajorProblem class LinkedList { LinkedList next; head } mid int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; tail head.next = mid; mid.next = tail; tail.next = head; 2 head = null; mid = null; tail = null; }
OneMajorProblem class LinkedList { LinkedList next; head } mid int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; tail head.next = mid; mid.next = tail; tail.next = head; 2 head = null; mid = null; tail = null; }
OneMajorProblem class LinkedList { LinkedList next; head } mid int main() { LinkedList LinkedList LinkedList head = new LinkedList; mid = new LinkedList; tail = new LinkedList; tail head.next = mid; mid.next = tail; tail.next = head; 2 head = null; mid = null; tail = null; }