1 / 135

Ga r b a g e Collection

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? . ●.

draco
Télécharger la présentation

Ga r b a g e Collection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GarbageCollection

  2. MemoryManagementSo Far Someitemsarepreallocatedandpersistthroughouttheprogram: ● What are they? Someare allocatedontheruntimestack: ● ● What are they? ● Someare allocatedintheheap: ● What are they? ● Howdowemanageheap-allocatedmemory? ●

  3. ManualMemoryManagement • OptionOne:Have programmerhandleheap allocationand deallocation C,C++ Advantages? Disadvantages? ● ●

  4. AutomaticMemoryManagement Idea:Runtime environmentautomaticallyreclaimmemory. What is the garbage? Advantages? ● ● Disadvantages?

  5. 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 */ }

  6. ApproximatingGarbage Ingeneral,undecidablewhetheranobjectisgarbage. Needtorelyonaconservativeapproximation. Anobject isreachableifitcanstillbereferencedbytheprogram. Garbage Collector - Detectandreclaimunreachableobjects.

  7. GC Assumptions Assumethat,atruntime,wecanfindall existingreferencesintheprogram. ● Cannotfabricateareferencetoanexistingobject Cannotcastpointerstointegersorvice-versa. ● ● Examples:Java,Python,JavaScript,PHP,etc. Non-examples:C,C++ ● ●

  8. 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?

  9. Major Approaches to GC • Reference Counting • Mark and Sweep • Stop and Copy • Generational

  10. ReferenceCounting Technique * Storeineachobjectareferencecount(refcount)trackinghowmanyreferencesexisttotheobject. * Createareferencetoanobject:incrementsrefcount. * Removeareferencetoanobject:decrementsrefcount. * Whenobjectrefcount==0,unreachable&reclaimed. Mightdecreaseotherobjects'refcountsandtriggermorereclamations. ●

  11. 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; }

  12. 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; }

  13. 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; }

  14. 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; }

  15. 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; }

  16. 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; }

  17. 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; }

  18. 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; }

  19. ReferenceCountinginAction class LinkedList { 1 head mid 0 head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  20. ReferenceCountinginAction class LinkedList { 1 head mid 0 head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  21. ReferenceCountinginAction class LinkedList { 1 head mid 1 head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  22. ReferenceCountinginAction class LinkedList { 1 head mid 1 head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  23. ReferenceCountinginAction class LinkedList { 1 head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  24. ReferenceCountinginAction class LinkedList { 1 head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 0 head.next.next = null; head = null; }

  25. ReferenceCountinginAction class LinkedList { 1 head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 0 head.next.next = null; head = null; }

  26. ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 1 head.next.next = null; head = null; }

  27. ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 1 head.next.next = null; head = null; }

  28. ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 2 head.next.next = null; head = null; }

  29. ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 2 head.next.next = null; head = null; }

  30. ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 2 head.next.next = null; head = null; }

  31. ReferenceCountinginAction class LinkedList { head mid tail head.next = mid; mid.next = tail; mid = tail = null; 1 head.next.next = null; head = null; }

  32. ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 0 head.next.next = null; head = null; }

  33. ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; 0 head.next.next = null; Reclaimed! head = null; }

  34. ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  35. ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  36. ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  37. ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  38. 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; }

  39. ReferenceCountinginAction class LinkedList { head mid 1 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  40. ReferenceCountinginAction class LinkedList { head mid 0 tail head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  41. ReferenceCountinginAction class LinkedList { head mid 0 tail Reclaimed! head.next = mid; mid.next = tail; mid = tail = null; head.next.next = null; head = null; }

  42. 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; }

  43. 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; }

  44. 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; }

  45. 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; }

  46. 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; }

  47. 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; }

  48. 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; }

  49. 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; }

  50. 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; }

More Related