Docsity

Garbage Collection: Understanding Different Methods and Algorithms, Slides of Compilers

Partial preview of the text

Download Garbage Collection: Understanding Different Methods and Algorithms and more Slides Compilers in PDF only on Docsity!

Garbage Collection

Reference Counting Mark-and-Sweep Short-Pause Methods

The Essence

  • Programming is easier if the run-time system “garbage-collects” --- makes space belonging to unusable data available for reuse. - Java does it; C does not. - But stack allocation in C gets some of the advantage.

The Model --- (1)

  • There is a root set of data that is a-priori reachable. - Example: In Java, root set = static class variables plus variables on run-time stack.
  • Reachable data : root set plus anything referenced by something reachable.
  • Question: Why doesn’t this make sense for C? Why is it OK for Java?

The Model --- (2)

  • Things requiring space are “objects.”
  • Available space is in a heap --- large area managed by the run-time system. - Allocator finds space for new objects. - Space for an object is a chunk. - Garbage collector finds unusable objects, returns their space to the heap, and maybe moves objects around in the heap.

Taxonomy

7

Garbage Collectors

Reference- Counters

Trace- Based

Reference Counting

  • The simplest (but imperfect) method is to give each object a reference count = number of references to this object. - OK if objects have no internal references.
  • Initially, object has one reference.
  • If reference count becomes 0, object is garbage and its space becomes available.

Transitive Effects

  • If an object reaches RC=0 and is collected, the references within that object disappear.
  • Follow these references and decrement RC in the objects reached.
  • That may result in more objects with RC=0, leading to recursive collection.

Example: Reference Counting

11

Root Object

A(1)

D(2) E(1)

B(2)

C(1)

Example: Reference Counting

13

Root Object

D(2) E(1)

B(1)

C(0)

Example: Reference Counting

14

Root Object

D(1) E(1)

B, D, and E are^ B(1) garbage, but their reference counts are all > 0. They never get collected.

Four States of Memory Chunks

1. Free = not holding an object; available for allocation. 2. Unreached = Holds an object, but has not yet been reached from the root set. 3. Unscanned = Reached from the root set, but its references not yet followed. 4. Scanned = Reached and references followed.

Marking

  1. Assume all objects in Unreached state.
  2. Start with the root set. Put them in state Unscanned. 3. while Unscanned objects remain do

examine one of these objects; make its state be Scanned; add all referenced objects to Unscanned if they have not been there; end ;

Taxonomy

19

Garbage Collectors Reference- Counters

Trace- Based Stop-the-World Short-Pause Mark-and- Sweep

Mark-and- Compact

Basic Baker’s

Baker’s Algorithm --- (1)

  • Problem: The basic algorithm takes time proportional to the heap size. - Because you must visit all objects to see if they are Unreached.
  • Baker’s algorithm keeps a list of all allocated chucks of memory, as well as the Free list.