garbage collection

24

Upload: michal-pise

Post on 21-Nov-2014

717 views

Category:

Education


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Garbage Collection
Page 2: Garbage Collection

The Purpose of Garbage Collection

Manual memory management is too tiresome and error-prone.

Memory leaks.Dangling pointers.

GC frees programmer to focus on more important issues.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 2 / 9

Page 3: Garbage Collection

A Few Definitions

Aim: to free objects that will not be used anymore.

That is computationally impossible.

Reachability: transitive closure of pointers starting with the root set(all global and local variables).

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3 / 9

Page 4: Garbage Collection

A Few Definitions

Aim: to free objects that will not be used anymore.

That is computationally impossible.

Reachability: transitive closure of pointers starting with the root set(all global and local variables).

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3 / 9

Page 5: Garbage Collection

A Few Definitions

Aim: to free objects that will not be used anymore.

That is computationally impossible.

Reachability: transitive closure of pointers starting with the root set(all global and local variables).

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3 / 9

Page 6: Garbage Collection

Reference Counting

Each object has a counter.

Reference assignment → decrease the counter of the old object andincrease the counter of the new one (if they exist).

When the counter reaches zero, free the object.

Disadvantages: fairly big overhead, can not deal with cycles.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4 / 9

Page 7: Garbage Collection

Reference Counting

Each object has a counter.

Reference assignment → decrease the counter of the old object andincrease the counter of the new one (if they exist).

When the counter reaches zero, free the object.

Disadvantages: fairly big overhead, can not deal with cycles.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4 / 9

Page 8: Garbage Collection

Reference Counting

Each object has a counter.

Reference assignment → decrease the counter of the old object andincrease the counter of the new one (if they exist).

When the counter reaches zero, free the object.

Disadvantages: fairly big overhead, can not deal with cycles.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4 / 9

Page 9: Garbage Collection

Reference Counting

Each object has a counter.

Reference assignment → decrease the counter of the old object andincrease the counter of the new one (if they exist).

When the counter reaches zero, free the object.

Disadvantages: fairly big overhead, can not deal with cycles.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4 / 9

Page 10: Garbage Collection

Mark and Sweep

Each object has a mark.

Once in a while, go through transitive closure of the root set andmark all objects along the way.

Then free all unmarked objects.

Disadvantages: fragmentation, locality of reference, cost isproportional to the size of available memory.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5 / 9

Page 11: Garbage Collection

Mark and Sweep

Each object has a mark.

Once in a while, go through transitive closure of the root set andmark all objects along the way.

Then free all unmarked objects.

Disadvantages: fragmentation, locality of reference, cost isproportional to the size of available memory.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5 / 9

Page 12: Garbage Collection

Mark and Sweep

Each object has a mark.

Once in a while, go through transitive closure of the root set andmark all objects along the way.

Then free all unmarked objects.

Disadvantages: fragmentation, locality of reference, cost isproportional to the size of available memory.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5 / 9

Page 13: Garbage Collection

Mark and Sweep

Each object has a mark.

Once in a while, go through transitive closure of the root set andmark all objects along the way.

Then free all unmarked objects.

Disadvantages: fragmentation, locality of reference, cost isproportional to the size of available memory.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5 / 9

Page 14: Garbage Collection

Mark and Compact, Mark and Copy

The same as mark and sweep except objects are moved (copied) sothat they are next to each other.

Disadvantages: costly when there is a large number of survivors.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 6 / 9

Page 15: Garbage Collection

Mark and Compact, Mark and Copy

The same as mark and sweep except objects are moved (copied) sothat they are next to each other.

Disadvantages: costly when there is a large number of survivors.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 6 / 9

Page 16: Garbage Collection

Incremental Collectors

Stop the world algorithms disrupt the execution of the program fortoo long.

It is better to collect garbage by increments.

However, incremental algorithms must accommodate for possiblechanges in the object graph.

Two basic ways: read barrier and write barrier.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7 / 9

Page 17: Garbage Collection

Incremental Collectors

Stop the world algorithms disrupt the execution of the program fortoo long.

It is better to collect garbage by increments.

However, incremental algorithms must accommodate for possiblechanges in the object graph.

Two basic ways: read barrier and write barrier.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7 / 9

Page 18: Garbage Collection

Incremental Collectors

Stop the world algorithms disrupt the execution of the program fortoo long.

It is better to collect garbage by increments.

However, incremental algorithms must accommodate for possiblechanges in the object graph.

Two basic ways: read barrier and write barrier.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7 / 9

Page 19: Garbage Collection

Incremental Collectors

Stop the world algorithms disrupt the execution of the program fortoo long.

It is better to collect garbage by increments.

However, incremental algorithms must accommodate for possiblechanges in the object graph.

Two basic ways: read barrier and write barrier.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7 / 9

Page 20: Garbage Collection

Generational Collectors

Most objects live a very short time.

GC among younglings has much higher efficiency—i. e. it lastsshorter and yields more free space.

Therefore, the whole memory is divided into regions, objects arepropagated through regions based on their age.

The root set for a given region has to include references from regionswith more mature objects.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8 / 9

Page 21: Garbage Collection

Generational Collectors

Most objects live a very short time.

GC among younglings has much higher efficiency—i. e. it lastsshorter and yields more free space.

Therefore, the whole memory is divided into regions, objects arepropagated through regions based on their age.

The root set for a given region has to include references from regionswith more mature objects.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8 / 9

Page 22: Garbage Collection

Generational Collectors

Most objects live a very short time.

GC among younglings has much higher efficiency—i. e. it lastsshorter and yields more free space.

Therefore, the whole memory is divided into regions, objects arepropagated through regions based on their age.

The root set for a given region has to include references from regionswith more mature objects.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8 / 9

Page 23: Garbage Collection

Generational Collectors

Most objects live a very short time.

GC among younglings has much higher efficiency—i. e. it lastsshorter and yields more free space.

Therefore, the whole memory is divided into regions, objects arepropagated through regions based on their age.

The root set for a given region has to include references from regionswith more mature objects.

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8 / 9

Page 24: Garbage Collection

See

Paul Wilson. Uniprocessor Garbage Collection Techniques. MemoryManagement. http://dx.doi.org/10.1007/BFb0017182

Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 9 / 9