Lecture 24Storage ManagementGarbage Collection: Reference CountingGarbage Collection: Mark and SweepCopying Garbage CollectionCopying Garbage Collection, IllustratedRoots and Other PointersConservative Garbage CollectionGenerational Garbage CollectionRegion-Based AllocationRegion ImplementationLecture 24Administrivia• I will be out of town from Friday through next Wednesday.• Tuesday lecture: various exciting topics from our TAs.• Enough project 3 files to code should be up tonight. Expect to mergelater.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 1Storage Management• Java has no means to free dynamic storage.• However, when no expression in any thread can possibly be influ-enced by or change an object, it might as well not exist:IntList wasteful (){IntList c = new IntList (3, new IntList (4, null));return c.tail;// variable c now deallocated, so no way// to get to first cell of list}• At this point, Java runtime, like Scheme’s, recycles the object cpointed to:garbage collection.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 2Garbage Collection: Reference Counting• Idea: Keep count of number of pointers to each object.X: 1 1 11 A 1 B 1 CY:X: 1 2 11 A 1 B 1 CY:Y = X.tail;X: 0 3 11 A 1 B 1 CY:X = Y;X: 2 10 A 1 B 1 Cetc.Y:Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 3Garbage Collection: Mark and SweepRoots5E B GD7C42AF42ADB*FCAD*7 G DE* FCG*B• Start at roots (named variables, static and on stack)• Perform graph traversal to find and mark all reachable storage.• Sweep over memory, adding all unmarked storage to free list.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 4Garbage Collection: Mark and SweepRoots5E B GD7C42AF42ADB*FCAD*7 G DE* FCG*B• Start at roots (named variables, static and on stack)• Perform graph traversal to find and mark all reachable storage.• Sweep over memory, adding all unmarked storage to free list.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 4Garbage Collection: Mark and SweepRoots5E B GD7C42AF42ADB*FCAD*7 G DE* FCG*B• Start at roots (named variables, static and on stack)• Perform graph traversal to find and mark all reachable storage.• Sweep over memory, adding all unmarked storage to free list.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 4Garbage Collection: Mark and SweepRoots5E B GD7C42AF42ADB*FCAD*7 G DE* FCG*B• Start at roots (named variables, static and on stack)• Perform graph traversal to find andmark all reachable storage.• Sweep over memory, adding all unmarked storage to free list.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 4Garbage Collection: Mark and SweepRoots5E B GD7C42AF42ADB*FCAD*7 G DE* FCG*B• Start at roots (named variables, static and on stack)• Perform graph traversal to find andmark all reachable storage.• Sweep over memory, adding all unmarked storage to free list.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 4Garbage Collection: Mark and SweepRoots5E B GD7C42AF42ADB*FCAD*7 G DE* FCG*B• Start at roots (named variables, static and on stack)• Perform graph traversal to find andmark all reachable storage.• Sweep over memory, adding all unmarked storage to free list.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 4Garbage Collection: Mark and SweepRoots5E B GD7C42AF42ADB*FCAD*7 G DE* FCG*B• Start at roots (named variables, static and on stack)• Perform graph traversal to find andmark all reachable storage.• Sweep over memory, adding all unmarked storage to free list.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 4Garbage Collection: Mark and SweepRoots5E B GD7DB* D*7 G DE* G*B• Start at roots (named variables, static and on stack)• Perform graph traversal to find andmark all reachable storage.• Sweep over memory, adding all unmarked storage to free list.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 4Copying Garbage Collection• Copy (and move) only reachable (useful) storage from ‘from’ spaceto ‘to’ space.• The ‘from’ and ‘to’ areas are calledsemispaces. Need twice the vir-tual memory you actually use.• As you copy, mark ‘from’ storage as moved, and leave behind afor-warding pointerthat tells how to translate other references to theold storage.• At end of algorithm, ‘from’ and ‘to’ swap roles, and the old ‘from’area is freeden masse.• Copied storage iscompacted(gaps squeezed out) with possible ad-vantages for memory access.Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 5Copying Garbage Collection, IllustratedRootsB5Efrom: 42ADBG FCAD7 G DEFCGBto:Roots5E B GD7C42AFLast modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Copying Garbage Collection, IllustratedRootsB’5E’from:42ABG FCAD7 GE FCGBto: DB’G DE’Roots5E B GD7C42AFE’ B’Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Copying Garbage Collection, IllustratedRootsB’5E’from:42ABG FCAD7 GE FCGBto: D’B’G DE’D’7 GRoots5E B GD7C42AFE’ B’D’7Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Copying Garbage Collection, IllustratedRootsB’5E’from:42ABG FCAD7 GE FCGBto: D’B’G’ DE’D’7 GG’BRoots5E B GD7C42AFE’ B’ G’D’7Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Copying Garbage Collection, IllustratedRootsB’5E’from:42ABG FCAD7 GE FCGBto: D’B’G’ D’E’D’7 GG’BRoots5E B GD7C42AFE’ B’ G’D’7Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Copying Garbage Collection, IllustratedRootsB’5E’from:42ABG FCAD7 GE FCGBto: D’B’G’ D’E’D’7 GG’BRoots5E B GD7C42AFE’ B’ G’D’7Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Copying Garbage Collection, IllustratedRootsB’5E’from:42ABG FCAD7 GE FCGBto: D’B’G’ D’E’D’7 G’G’BRoots5E B GD7C42AFE’ B’ G’D’7Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Copying Garbage Collection, IllustratedRootsB’5E’from:42ABG FCAD7 GE FCGBto: D’B’G’ D’E’D’7 G’G’B’Roots5E B GD7C42AFE’ B’ G’D’7Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Copying Garbage Collection, IllustratedRootsB’5E’to:from:D’B’G’ D’E’D’7 G’G’B’Roots5E’ B’ G’D’7Last modified: Thu Apr 16 10:58:23 2009 CS164: Lecture #24 6Roots and Other Pointers• Above methods require that we know locations of roots and of pointerfields in objects.• Positions of some roots change during execution.•
View Full Document