1.39k likes | 1.62k Views
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:
E N D
Smalltalk Implementation:Memory Managementand 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: • 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”
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:”
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
root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e
root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e
root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e
root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e
root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e
root Example a • Mark every reachable object • starting with the root object(s). c b g d f h e
root Example a • Everything else is garbage • Delete the garbage • “reclaim the memory space” c b g d f h e
root Example a • Everything else is garbage • Delete the garbage • “reclaim the memory space” c g d f
root Example a • Step 2: Compact the memory. c g d f
root Example a • Step 2: Compact the memory. c b g d f h e
root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory
root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory
root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory
root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory
root Example h a • Step 2: Compact the memory. g c b g d f f h e e d c b a Memory
root Example h a • Step 2: Compact the memory. g c b g d f h e e f d c b a Memory
root Example a • Step 2: Compact the memory. c b g d f h e g f d c b a Memory
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.
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, …
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.
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:
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:
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”.
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
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
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
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
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
Baker’s Semi-Space Algorithm root object • During normal operation a b c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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