Your SlideShare is downloading. ×
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Compiling Imperative and Object-Oriented Languages - Garbage Collection

970

Published on

Presentation slides for lecture 10 of course IN4303 on Compiler Construction at TU Delft.

Presentation slides for lecture 10 of course IN4303 on Compiler Construction at TU Delft.

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
970
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. Garbage CollectionGuido Wachsmuth Delft Course IN4303 University of Technology Compiler Construction Challenge the future
    • 2. Recapregister allocationHow can we assign registers to local variables and temporaries? • perform liveness analysis • build interference graph • colour interference graphWhat to do if the graph is not colourable? • keep local variables in memoryHow to handle move instructions efficiently? • coalesce nodes safely Garbage Collection 2
    • 3. Overviewtoday’s lecturegarbage collection Garbage Collection 3
    • 4. Overviewtoday’s lecturegarbage collectionmark & sweep • mark reachable records • sweep unmarked records Garbage Collection 3
    • 5. Overviewtoday’s lecturegarbage collectionmark & sweep • mark reachable records • sweep unmarked recordsreference counting • deallocate records with count 0 Garbage Collection 3
    • 6. Overviewtoday’s lecturegarbage collectionmark & sweep • mark reachable records • sweep unmarked recordsreference counting • deallocate records with count 0copy collection • copy reachable records • consider generations of records Garbage Collection 3
    • 7. Igarbage collection Garbage Collection 4
    • 8. the heap Garbage Collection 5
    • 9. Recap: Java Virtual Machinethe heap method area stackpc: 00 constant pool optop: 00 local variables00 12 ldc 00 4303 4303 00 00 002A 002A01 00 00 01 0000 0004 01 0102 19 aload 02 02 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
    • 10. Recap: Java Virtual Machinethe heap method area stackpc: 00 02 constant pool optop: 00 01 local variables00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A01 00 00 01 0000 0004 01 0102 19 aload 02 02 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
    • 11. Recap: Java Virtual Machinethe heap method area stackpc: 00 04 02 constant pool optop: 00 02 01 local variables00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A01 00 00 01 0000 0004 01 002A 002A 0102 19 aload 02 02 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
    • 12. Recap: Java Virtual Machinethe heap method area stackpc: 00 06 04 02 constant pool optop: 00 03 02 01 local variables00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A01 00 00 01 0000 0004 01 002A 002A 0102 19 aload 02 02 0000 0004 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
    • 13. Recap: Java Virtual Machinethe heap method area stackpc: 00 07 06 04 02 constant pool optop: 00 02 01 local variables00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A01 00 00 01 0000 0004 01 002A 002A 0000 0042 0102 19 aload 02 02 0000 0004 0203 00 00 03 03 0304 12 ldc 04 04 0405 01 01 05 05 0506 2E iaload 06 06 06 heap4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
    • 14. Heap example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 7
    • 15. IImark & sweep Garbage Collection 8
    • 16. Mark & sweepideamark • mark reachable records • start at variables (roots) • follow referencessweep • marked records: unmark • unmarked records: deallocate • linked list of free records Garbage Collection 9
    • 17. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 10
    • 18. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 19. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 20. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 21. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 22. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 23. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 24. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 25. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 26. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 27. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 28. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 29. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 30. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 31. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 32. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 33. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 34. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 35. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 36. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 37. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 38. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 39. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 40. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 41. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 42. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 43. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 44. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 45. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 46. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 47. Marking example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
    • 48. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 11
    • 49. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 50. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 51. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 52. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 53. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 54. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 55. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 56. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 57. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 58. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 59. Sweeping example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
    • 60. Markingalgorithmsfunction DFS(x) function DFS(x) if pointer(x) & !marked[x] if pointer(x) & !marked[x] marked[x] = true marked[x] = true t = 1 ; stack[t] = x foreach f in fields(x) while t > 0 DFS(f) x = stack[t] ; t = t - 1 foreach f in fields(x) if pointer(f) & !marked[f] marked[f] = true t = t + 1 ; stack[t] = f Garbage Collection 12
    • 61. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 62. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 63. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 64. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 65. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 66. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 67. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 68. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 69. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 70. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 71. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 72. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 73. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 74. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 75. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 76. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 77. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 78. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 79. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 80. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 81. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 82. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 83. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 84. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 85. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 86. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 87. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 88. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 89. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 90. Recap: Marking pointer reversals12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
    • 91. Mark & sweepnotessweeping • independent of marking algorithm • several freelists (per record size) • split free records for allocationfragmentation • external: many free records of small size • internal: too-large record with unused memory inside Garbage Collection 14
    • 92. IIIreference counts Garbage Collection 15
    • 93. Reference countsideacounts • how many pointers point to each record? • store with each recordcounting • extra instructionsdeallocate • put on freelist • recursive deallocation on allocation Garbage Collection 16
    • 94. Reference counts example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 17
    • 95. Reference counts example12 1 ● ● 15 1 ● ● ●17 37 2 ● ● 59 1 ● ● ●19 20 1 ● ● ● 37 ● p q r Garbage Collection 17
    • 96. Reference counts example12 1 ● ● 15 1 ● ● ●17 37 2 ● ● 59 1 ● ● ●19 20 1 ● ● ● 37 ● p q r Garbage Collection 17
    • 97. Reference countsnotescycles • memory leaks • break cycles explicitly • occasional mark & sweep collectionexpensive • fetch, decrease, store old reference counter • possible deallocation • fetch, increase, store new reference counter Garbage Collection 18
    • 98. coffee break Garbage Collection 19
    • 99. IVcopy collections Garbage Collection 20
    • 100. Copy collectionsideaspaces • fromspace & tospace • switch roles after copycopy • traverse reachability graph • copy from fromspace to tospace • fromspace unreachable, free memory • tospace compact, no fragmentation Garbage Collection 21
    • 101. Copy collection example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 22
    • 102. Copy collection example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 22
    • 103. Copy collection example12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● Garbage Collection 22
    • 104. Copy collection example12 ● ● 15 ● ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● Garbage Collection 22
    • 105. Copy collection example12 ● ● 15 ● ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● Garbage Collection 22
    • 106. Copy collection example12 ● ● 15 ● ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● Garbage Collection 22
    • 107. Copy collection example12 ● ● 15 ● ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● Garbage Collection 22
    • 108. Copy collection example12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● Garbage Collection 22
    • 109. Copy collection example12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● Garbage Collection 22
    • 110. Copy collection example12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● Garbage Collection 22
    • 111. Copy collection12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● Garbage Collection 23
    • 112. Copy collection12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● Garbage Collection 23
    • 113. Copy collection12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● Garbage Collection 23
    • 114. Copy collection12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
    • 115. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
    • 116. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
    • 117. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
    • 118. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
    • 119. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
    • 120. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
    • 121. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● Garbage Collection 23
    • 122. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● Garbage Collection 23
    • 123. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● Garbage Collection 23
    • 124. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● Garbage Collection 23
    • 125. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 126. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 127. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 128. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 129. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 130. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 131. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 132. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 133. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 134. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 135. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 136. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 137. Copy collection12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 138. Copy collection ● 37 ● p q r15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
    • 139. Copy collectionsalgorithmsfunction Forward(p) function BFS() if fromspace(p) next = scan = start(tospace) if tospace(fields(p)[1]) return fields(p)[1] foreach r in root() r = Forward(r) else while scan < next foreach f in fields(p) next.f = f foreach f in fields(scan) f = Forward(f) fields(p)[1] = next next = next + size(p) scan = scan + size(scan) return fields(p)[1] else return p Garbage Collection 24
    • 140. Copy collectionslocalityadjacent records • likely to be unrelatedpointers to records in records • likely to be accessed • likely to be far apartsolution • depth-first copy: slow pointer reversals • hybrid copy algorithm Garbage Collection 25
    • 141. Copy collectionsgenerational collectiongenerations • young data: likely to die soon • old data: likely to survive for more collections • divide heap, collect younger generations more frequentlycollection • roots: variables & pointers from older to younger generations • preserve pointers to old generations • promote objects to older generations Garbage Collection 26
    • 142. Vsummary Garbage Collection 27
    • 143. Summarylessons learnedHow can we collect unreachable records on the heap? • reference counts • mark reachable records, sweep unreachable records • copy reachable recordsHow can we reduce heap space needed for garbage collection? • pointer-reversal • breadth-first search • hybrid algorithms Garbage Collection 28
    • 144. Literaturelearn moreAndrew W. Appel, Jens Palsberg: Modern CompilerImplementation in Java, 2nd edition. 2002 Garbage Collection 29
    • 145. Outlookcoming nextcompiler components and their generators • Lecture 11: Lexical Analysis • Lecture 12: Syntactical Analysis • Lecture 13: SDF inside • Lecture 14: Static AnalysisLab Nov 19 & Nov 26 • test cases • finish Lab 2, do reference resolution next • finish Lab 3, do hover help next • content completion Garbage Collection 30
    • 146. questions Garbage Collection 31
    • 147. credits Garbage Collection 32
    • 148. PicturescopyrightsSlide 1: Trash by Vladimer Shioshvili, some rights reservedSlide 5: Gravel Pile by kenjonbro, some rights reservedSlide 19: Typhoo by Dominica Williamson, some rights reservedSlide 30: Romantic Pigeon Date by Harald Hoyer, some rights reservedSlide 31: Questions by Oberazzi, some rights reservedSlide 32: Too Much Credit by Andres Rueda, some rights reserved Garbage Collection 33

    ×