1 / 138

Smalltalk Implementation: Memory Management and Garbage Collection

Smalltalk Implementation: Memory Management and Garbage Collection. Prof. Harry Porter Portland State University. The Object Manager. A separate section of the VM. Encapsulates all memory management. Includes the garbage collector. Interface from rest of VM:

saburo
Télécharger la présentation

Smalltalk Implementation: Memory Management and Garbage 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. Smalltalk Implementation:Memory Managementand Garbage Collection • Prof. Harry Porter • Portland State University

  2. The Object Manager • A separate section of the VM. • Encapsulates all memory management. • Includes the garbage collector. • Interface from rest of VM: • Called to allocate new space, new objects • May impose constraints on • pointer dereferencing • (i.e., chasing pointers, fetching OOPs from object memory) • pointer stores • (i.e., copying an OOP into a variable) • Garbage Collector… • Called implicitly when allocating new objects • No more free space? Run the garbage collector. • Try again • Still not enough space? Crash! • May by called periodically to “keep on top of the problem”

  3. Object Manager Interface • • Create a new object • • Retrieve an object’s field • • Update an object’s field • • Get an object’s size • • Get an object’s class pointer • • Support “become:” operation • • Enumerate objects… “allInstancesDo:”

  4. root Example a • The “root” object • Defines what is reachable • May be several root pointers • • From the calling stack • • Registers, etc. c b g d f h e

  5. root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e

  6. root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e

  7. root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e

  8. root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e

  9. root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e

  10. root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e

  11. root Example a • Everything else is garbage • Delete the garbage • “reclaim the memory space” c b g d f h e

  12. root Example a • Everything else is garbage • Delete the garbage • “reclaim the memory space” c g d f

  13. root Example a • Step 2: Compact the memory. c g d f

  14. root Example a • Step 2: Compact the memory. c b g d f h e

  15. root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory

  16. root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory

  17. root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory

  18. root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory

  19. root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory

  20. root Example h a • Step 2: Compact the memory. g c b g d f h e e f d c b a Memory

  21. root Example a • Step 2: Compact the memory. c b g d f h e g f d c b a Memory

  22. Just Use Virtual Memory??? • Idea: Avoid G.C. and just use virtual memory • Page objects out to disk. • Worry about collecting later (perhaps at night?) • Smalltalk Statistics: • Average size of new objects: 20 bytes • Minimum object size: 4 bytes • Object allocation rate: • 1 object per 80 bytecodes executed • (= 1/4 bytes allocated per bytecodes executed) • The Numbers: • Execution rate: 4,000,000 bytecodes/sec • Disk Size: 10 Gbyte • Result: Disk fills up in 80 minutes • (And how long to collect 10 Gbyte on disk?) • Conclusion:We cannot ignore G.C.

  23. Major Garbage Collection Algorithms • • Mark-Sweep • Simple • • Baker’s Semi-Space Algorithm • Good intro. to Generation Scavenging • • Generation Scavenging (David Ungar) • Fast • Widespread use • • Reference Counting • No longer used in Smalltalk • Ongoing research: • Performance tuning, variations, …

  24. Mark-Sweep Garbage Collection • Associate a single bit with each object • The “mark” bit • Part of the object’s header • Initially, all “mark” bits are clear • • Phase 1: • Set the “mark” bit for every reachable object • • Phase 2: • Compact the object space • (and clear the “mark” bit for next time) • Will move objects. • Need to adjust all pointers.

  25. Mark-Sweep Garbage Collection • How to set the “mark” bit? • Option 1: A recursive algorithm • But this requires a stack (and memory is full!) • Option 2: • Option 3:

  26. Mark-Sweep Garbage Collection • How to set the “mark” bit? • Option 1: A recursive algorithm • But this requires a stack (and memory is full!) • Option 2: • REPEAT • LOOP through all objects • IF the object’s mark is set THEN • LOOP through the object’s fields • Set the mark bit of all objects it points to • ENDLOOP • ENDIF • ENDLOOP • UNTIL no more changes • Repeated loops through memory? SLOW! • Option 3:

  27. Mark-Sweep Garbage Collection • How to set the “mark” bit? • Option 1: A recursive algorithm • But this requires a stack (and memory is full!) • Option 2: • REPEAT • LOOP through all objects • IF the object’s mark is set THEN • LOOP through the object’s fields • Set the mark bit of all objects it points to • ENDLOOP • ENDIF • ENDLOOP • UNTIL no more changes • Repeated loops through memory? SLOW! • Option 3: • Keep a “to-do list”.

  28. Mark-Sweep Garbage Collection • Desired Algorithm: • When we mark an object, push it on a stack. • Repeat: Pop next object off of stack • Mark all reachable objects • … until stack is empty • Unfortunately: • The stack may be arbitrarily deep. • No extra memory when the G.C. is running! • Solution: • Allocate one extra word per object. • Use this “extra” pointer to maintain a linked list of objects • (the stack) • When an object is found to be reachable... • Set its “mark” bit • Add it to the linked list extra ptr size/flags class header

  29. Mark-Sweep Garbage Collection • Mark root object • Add root object to the linked list • LOOP • Remove an element from the list • Look at each of its fields... • FOR EVERY object it points to • IF it is not already marked THEN • Mark it • Add it to the list • ENDIF • ENDFOR • UNTIL list is empty extra ptr size/flags class header

  30. Mark-Sweep Garbage Collection • Advantages: • • Will identify all true garbage • • Very little space overhead • • Simple  Easy to program • Disadvantages: • • The marking phase can be slow! • - Must look at every field • (in every non-garbage object) • - Must check the “tag” bit • OOP  follow the pointer • SmallInteger  ignore • • Causes lengthy interruptions (periodically) • Annoying for interactive applications

  31. Baker’s Semi-Space Algorithm • Memory is divided into 2 (equal-sized) spaces • FROM-SPACE • TO-SPACE • Normal Operation: • • All objects are in FROM-SPACE • • TO-SPACE is unused • • New objects are allocated in FROM-SPACE • (typically like a stack) • When FROM-SPACE is exhausted… next FROM- SPACE TO- SPACE

  32. Baker’s Semi-Space Algorithm • Memory is divided into 2 (equal-sized) spaces • FROM-SPACE • TO-SPACE • Normal Operation: • • All objects are in FROM-SPACE • • TO-SPACE is unused • • New objects are allocated in FROM-SPACE • (typically like a stack) • When FROM-SPACE is exhausted… • • Copy the root object to TO-SPACE • • Copy all reachable objects • from the FROM-SPACE • to the TO-SPACE • • All the garbage objects are left behind in FROM-SPACE • • Abandon FROM-SPACE and continue processing in TO-SPACE next FROM- SPACE TO- SPACE

  33. Baker’s Semi-Space Algorithm root object • During normal operation a b c

  34. Baker’s Semi-Space Algorithm root object • During normal operation • Use one pointer in FROM-SPACE • next-free-location a b c next free location FROM-SPACE

  35. Baker’s Semi-Space Algorithm root object • During normal operation • Use one pointer in FROM-SPACE • next-free-location a b c next free location b d a c e root object FROM-SPACE

  36. Baker’s Semi-Space Algorithm root object • During normal operation • Use one pointer in FROM-SPACE • next-free-location a b c next free location b d a c e root object FROM-SPACE

  37. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. a b c next free location b d a c e root object FROM-SPACE

  38. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. a b c b d a c e next free location root object root object next unscanned location FROM-SPACE TO-SPACE

  39. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. a b c b d a c e next free location root object root object next unscanned location FROM-SPACE TO-SPACE

  40. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. a b c b d a next free location c a e root object root object next unscanned location FROM-SPACE TO-SPACE

  41. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. a b c b d next free location a b c a e root object root object next unscanned location FROM-SPACE TO-SPACE

  42. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. a b c b d next free location a b c a next unscanned location e root object root object FROM-SPACE TO-SPACE

  43. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. a b c b d next free location c a b c a next unscanned location e root object root object FROM-SPACE TO-SPACE

  44. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. a b c b d next free location c a b next unscanned location c a e root object root object FROM-SPACE TO-SPACE

  45. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. • Until the pointers meet. a b c b d next free location c a b next unscanned location c a e root object root object FROM-SPACE TO-SPACE

  46. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. • Until the pointers meet. a b c b d next free location c a b c a e root object root object FROM-SPACE TO-SPACE

  47. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. • Until the pointers meet. • Then swap spaces. a b c b d next free location c a b c a e root object root object FROM-SPACE TO-SPACE

  48. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. • Until the pointers meet. • Then swap spaces. a b c b d next free location c a b c a e root object root object TO-SPACE FROM-SPACE

  49. Baker’s Semi-Space Algorithm root object • During garbage collection… • Copy all reachable objects to TO-SPACE • First copy the root object. • Then scan the next object • and copy the objects it points to. • Until the pointers meet. • Then swap spaces. a b c next free location c b a root object TO-SPACE FROM-SPACE

  50. Baker’s Semi-Space Algorithm • Details: • We also need to update all the pointers in the objects. • Whenever we copy an object… • Leave a “forwarding pointer” behind in the old object. • Point to the copy in TO-SPACE. • Storage overhead? • OK to overwrite other fields (e.g., size, class) • Will need one bit per object • 0 = object not copied (yet) • 1 = object moved; use forwarding pointer

More Related