+ All Categories
Home > Documents > Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection...

Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection...

Date post: 03-Aug-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
76
Transcript
Page 1: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Garbage Collection

Page 2: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Garbage Collection

Garbage collection automatically frees storage which is notused by the program any more.

Has two phases:

{ Garbage detection | �nds which objects are alive andwhich dead;

{ Garbage reclamation | unallocates dead objects.

Liveness is a global semantic property which is unsolvablein general.

Garbage collection uses an approximation: an object is aliveif it's reachable from the root set; otherwise it's dead.

Page 3: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Reference-Counting

Reference-Counting

Each object has a counter which keeps track the number ofreferences to the object.

Counter is modi�ed when references to the object areadded/deleted:

{ counter is incremented on adding a new refernce;{ counter is decremented on deletion of a reference.

If counter is zero, then the object is freed:

{ the object is inserted into the free list;{ all its outgoing pointers are deleted.

Page 4: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Reference-Counting

Example:

1 2 1

1 1

1

2

1 1

HEAP SPACE

ROOTSET

Page 5: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Reference-Counting

Example:

2 2 1

1 1

1

2

0 1

HEAP SPACE

ROOTSET

Page 6: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Reference-Counting

Example:

2 2 1

1 1

1

2

0

HEAP SPACE

ROOTSET

Page 7: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Reference-Counting

Example:

2 2 1

1 1

1

1

HEAP SPACE

ROOTSET

Page 8: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Reference-Counting

Advantages

4 simple to implement;

4 activities related to garbage collection are distributed:

{ relatively easy to make it incremental;

4 good locality:

{ modi�es only counters of source and target references;

4 minimal zombie time (time between the object becoming agarbage and its reclamation);

4 allows easy implementation of object �nalization.

Page 9: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Reference-Counting

Drawbacks

8 relatively ine�cient:

{ must manage counters even when there is no garbage.

8 memory fragmentation:

{ analoguous to other free list based methods;

8 if there are many small objects, may require substantialamount of memory for counters.

8 the complexity of recursive unalloaction is in worst casebounded by size of the heap;

8 is unable to reclaim all garbage:

{ cyclic data structures.

Page 10: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Mark-Sweep

Has two phases:

1 starting from roots, mark all reachable objects;

2 scans over the heap and free all objects which are notmarked.

void gc (void) {foreach x 2 Roots do

mark (x);

end;

collect ();

}

Page 11: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Procedure mark()

Marks the given node and then recursively marks all nodesreachable from it.

Recursion stops when the node is already marked or if thenode contains only primitive values (no pointers).

void mark (ref x) {if (x!mark == 0) {x!mark = 1;

foreach y 2 sons(x) do

mark (y);

end;

}}

Page 12: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Procedure collect()

Performs a full scan over the heap and puts all unmarkedobjects into the free list.

void collect (void) {freelist = NIL;

foreach x 2 objects() do

if (x!mark == 0) {x!next = freelist;

freelist = x;

}else x!mark = 0;

end;

}

Page 13: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

0 0 0 0 0

Page 14: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

1 0 0 0 0

Page 15: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

1 0 1 0 0

Page 16: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

1 0 1 0 1

Page 17: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

1 0 1 0 1

Page 18: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

1 0 1 0 1

Page 19: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

1 0 1 0 1

FL

Page 20: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

0 0 1 0 1

FL

Page 21: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

0 0 1 0 1

FL

Page 22: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

0 0 0 0 1

FL

Page 23: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

0 0 0 0 1

FL

Page 24: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Example:

1 Recursive marking:

2 Collecting the garbage:

0 0 0 0 0

FL

Page 25: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Sweep garbage collection

Drawbacks

8 Marking is recursive.

{ In the worst case, size of the recursion stack is linear tosize of the heap!!

{ Possible solution: Deutsch-Schorr-Waite pointer reversalalgorithm.

8 Live objects are mixed with free heap areas.

{ Memory fragmentation.{ Possible solution: Mark-Compact garbage collection.

Page 26: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

Deutsch-Schorr-Waite algorithm

void mark (ref x) {FP = x; BP = NIL;

while (FP!mark 6= -1 || BP 6= NIL) {if (FP!mark == 0) {FP!mark = i = nextidx(FP);

if (i 6= -1) {tmp = FP; FP = tmp[i];

tmp[i] = BP; BP = tmp;

}} else { // FP!mark 6= 0...

}}

}

Page 27: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

0

i

BP

FP

i

i

BP

FP

Page 28: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

Deutsch-Schorr-Waite algorithm

...

} else { // FP!mark 6= 0i = nextidx(BP);

if (i 6= -1) {tmp = FP; FP = BP[i]; BP[i] = BP[BP!mark];

BP[BP!mark] = tmp; BP!mark = i;

} else {tmp = FP; FP = BP; BP = FP[FP!mark];

FP[FP!mark] = tmp; FP!mark = i;

}}...

Page 29: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

i

i j

BP

FP

j

i j

BP

FP

Page 30: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

j

i j

BP

FP

-1

i j

BP

FP

Page 31: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

0 0 0 0 0

FP

BP

Page 32: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

1 0 0 0 0

FP

BP

Page 33: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

1 0 -1 0 0

FP

BP

Page 34: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

2 0 -1 0 0

FP

BP

Page 35: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

2 0 -1 0 1

FP

BP

Page 36: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

2 0 -1 0 -1

FP

BP

Page 37: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Pointer Reversal

-1 0 -1 0 -1

FP

BP

Page 38: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Mark-Compact garbage collection

Mark-Compact

Has three phases:

1 starting from roots, mark all reachable objects (similarlyfor Mark-Sweep);

2 perform full scan of the heap and compute new addressesfor marked objects;

3 move marked objects to their new locations and changepointers accordingly.

4 At the end of the garbage collection all free memory formsa single compact region in the heap.

8 Relatively ine�cient, as it requires several scans over theheap.

Page 39: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

Copying

The heap is divided into two equal subregions: FromSpaceand ToSpace.

FromSpace is a currently active memory region to whereallocated objects are saved.

Garbage collection is invoked when FromSpace becomesfull:

{ live objects are copied from FromSpace to ToSpace;{ FromSpace and ToSpace ip the roles (ie. formerToSpace becomes FromSpace and vice versa).

Page 40: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE TOSPACEROOTSET

Page 41: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE TOSPACEROOTSET

Page 42: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

TOSPACE FROMSPACEROOTSET

Page 43: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

Cheney algorithm

Has two (interchanging) phases:

the �rst phase (evacuate) copies all directly reachableobjects from FromSpace to ToSpace, replaces used pointersby the ones pointing to the new corresponding objects,and installs forwarding pointers in places of the evacuatedobjects;

the second phase (scavenge) linearly scans the objectscopied into ToSpace and all objects (in FromSpace) directlyreachable from them are evacuated; if the object has alreadybeen evacuated before, then it is not copied again but thepointer to it is replaced by the forwarding pointer;

the process ends, when all objects in ToSpace are scanned.

Page 44: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

FreeScan

Page 45: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

A B

FreeScan

Page 46: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

A B C

FreeScan

Page 47: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

A B C D

FreeScan

Page 48: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

A B C D E

FreeScan

Page 49: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

A B C D E F

FreeScan

Page 50: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

A B C D E F G

FreeScan

Page 51: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

A B C D E F G

FreeScan

Page 52: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

FROMSPACE

TOSPACE

ROOT SET

A B

C

D E

F

G

A B C D E F G

FreeScan

Page 53: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

Advantages

4 all free memory is in a single compact region;

4 object creation is very cheap:

{ memory allocation is an incrmentation of the heappointer by the object size;

{ checking of the heap exhaustion is a comparision of twopointers;

4 only live objects are inspected:

{ most objects have relatively short life span;{ hence, usually there are much less live objects thangarbage;

4 theoretical amortized e�ciency is very good:

{ on inrease of the heap size, the cost of copying will nearto zero!

Page 54: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Copying garbage collection

Drawbacks

8 the whole work is concentrated to the garbage collectiontime:

{ might result for annoying pauses;

8 breath-�rst traversal may mix locality patterns;

8 all pointers are rearranged:

{ might invalidate some invariants the program isassuming;

8 half of the memory is "useless";

8 objects with long life span are copied over and over again:

{ might be quite costly if "veteran" objects are large.

Page 55: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Empirical facts

Infant mortality { most objects have very short life span.Usually 80-90% objects die before the next megabyte isused:

{ 60-90% CL and 75-95% Haskell objects die beforegetting "10 kb old".

{ SML/NJ frees 98% of objects during each garbagecollection.

{ 95% of Java objects are "short-lived".

The older the object, the more probable that it survivesthe next garbage collection.

Directionality of reference { usually younger objects pointto the older ones.

Page 56: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Generational garbage collection

Memory is divided by the age of objects living there intogenerations.

The number and size of di�erent generations is usually�xed beforehand.

New objects (infants) are created into the youngestgeneration (nursery).

When alive objects get older (tenure) they are promoted tothe next generation.

Garbage collections of di�erent generations are done indi�erent frequencies

{ most frequently in the youngest generation.

Page 57: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Memory division into generations

Generation 1 (youngest)

Generation 2

Generation n (oldest)

...

Live object

Dead object

Page 58: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Remembered sets

In addition to "normal" roots, the given generation mayhave outside pointers from other generations.

Their locations can't be detrmined statically .

Dynamically searching possible roots from othergenerations during garbage collection is very costly.

Hence, each generation has a corresponding rememberedset, which contains references from other generations

{ if there is a pointer from one generation to another, thenthe reference is added into the remembered set of thetarget generation.

Page 59: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Remembered sets

Root set

Younggeneration

Rememberedset

Oldgeneration

Rememberedset

Page 60: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Problem

Remembered sets may require a signi�cant amount ofmemory

{ all intergeneration dependencies must be recorded.

Remembered sets must be mantained during the programexecution which may be very costly

{ each pointer variable may potentially beintergenerational.

Solution

Record in remembered sets only references from the oldergeneration to younger ones

{ in case of two generations, only one remembered set (forthe nursery) is needed.

Use approximate remembered sets.

Page 61: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

One-way remembered sets

Root set

Younggeneration

Rememberedset

Oldgeneration

Page 62: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Remembered sets

Pointers from an older to a younger generation are rootsfor the younger generation:

{ such pointers are relatively infrequent;{ they may be created only by destructively updating apointer in a tenure object;

{ such assignements are catched by write barriers.

Pointers from a younger to an older generation arefrequent:

{ not a problem, if garbage collection of the oldergeneration always collects also the younger one.

Page 63: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Generational garbage collection

Usually there are just two generations and the younger oneis relatively small.

Normally, garbage collection performs only a minorcollection which:

{ removes garbage only from the nursery;{ old enough objects are promoted to the tenured space.

When the tenured space is exhausted, a major collection isperformed; ie. garbage is collected from both generations.

Minor and major collections may use di�erent garbagecollection methods (eg. minor uses copying and major usesmark-compact).

Page 64: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Issues

Minor colections doesn't remove garbage in the tenuredspace:

{ all young objects pointed by a tenured garbage willremain uncollected (nepotism).

How old must be an object before promoting?

{ One minor collection is not enough, as objects createdjust before the collection haven't yet had time to die.

{ Usually, two minor collections is considered to beenough.

How large should be the nursery?

{ Must �t into the main memory.{ Too big may result to too long minor collection pauses.{ Too small doesn't give enough time for young objects todie.

Page 65: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm

Major collection may result to too long pauses forinteractive programs.

Train algorithm by Hudson and Moss uses incrementalcollection for the old generation.

The tenured space is divided into cars:

{ each car has its own remembered set;{ only one care is collected at once.

As substructures may live in di�erent cars, the cars aregrouped into trains:

{ the aim is to accumulate related data structures into onetrain.

Page 66: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | division of the tenured space

Old generation

Train 1

Train 2

Train 3

Car 1.1 Car 1.2 Car 1.3

Car 2.1 Car 2.2

Car 3.1 Car 3.2 Car 3.3 Car 3.4

Page 67: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm

Each call of the algorithm frees the �rst car (FromCar) ofthe �rst train (FromTrain).

If FromTrain doesn't have any outside pointers to it, thewhole train will be freed.

Otherwise, the objects in FromCar pointed from othertrains are evacuated into these trains; objects pointed fromother generations are evacuated into some other (may becompletely new) train.

Reimaining outside pointers of FromCar are from othercars of FromTrain; corresponding objects are evacuatedinto the last car of FromTrain (creating a new car ifnecessary), after which FromCar is freed.

Page 68: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | the initial state

Root set

Train 1

Train 2

R

A C

S

D E

T

F

B

Page 69: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | the state after the �rst collection

Root set

Train 1

Train 2

S

D E

T

F C

B

R

A

Page 70: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | the state after the second collection

Root set

Train 1

Train 2

T

F C

D E

B

R

A

S

Page 71: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | the state after the third collection

Root set

Train 1

Train 2

F C

D E

B

R

A

S T

Page 72: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | the state after the fourth collection

Root set

Train 1

Train 2

B

R

A

S T

Page 73: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | the state after the �fth collection

Root set

Train 2

Train 3

T

R S

Page 74: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | the state after the sixth collection

Root set

Train 2

Train 3

R S

T

Page 75: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Train algorithm | conclusion

4 If structures without outside pointers are completely in asingle train, they can be freed immediately.

4 In each collection, the number of evacuated objects isbounded by size of a single car.

4 Evacuated objects are compacted into a single train.

8 Relatively complicated.

8 Requires quite a lot of memory for remembered sets.

Page 76: Garbage Collectionkodu.ut.ee/~varmo/TM2008/slides/tm-gc.pdfMark-Compact garbage collection Mark-Compact Has three phases: 1 starting from roots, mark all reachable objects (similarly

Generational garbage collection

Advantages of generational garbage collection

4 Very successful for many applications.

4 Often shortens garbage collection pauses into the leveltolerable for interactive applications.

4 Has good locality properties.

4 Usually decreases the total garbage collection time.

Drawbacks of generational garbage collection

8 Worst case e�ciency is worse than in simpler methods.

8 Objects may not die fast enough.

8 Applications may be "hindered" by write barriers.

8 Too many old pointers into young objects, or too deepstack, may result longer pauses.


Recommended