Skip to content
Matt Basta edited this page Mar 30, 2014 · 2 revisions

Garbage collection in BType uses a reference counting approach. This avoids requiring stop-the-world GC pauses.

Because reference counting does not support cyclical references, BType implements a concept referred to as "finalizers". A finalizer is a function which manages the cleanup and dereferencing of an object. There are two types of finalizers:

  1. Dereferencing Finalizers
  2. Cleanup Finalizers

Both of these approaches are used for complex objects. The type of finalizer that is generated for an object (and consequently how that finalizer is called) is decided at compile time and is determined by the shape of the object and the type of members that it contains.

Dereferencing Finalizers

A dereferencing finalizer is used for any object which could possibly have an indirect reference to itself. Consider the following case:

class Foo {
    Bar:step1;
    func new() {}
}

class Bar {
    Foo:step2;
    func new() {}
}

In the above example, Foo can indirectly reference itself by holding a reference to a Bar object that references it. As such, a Foo with only a reference to a Bar with a reference pointing back will never achieve a reference count of 0 and be collected.

When an object like this is detected, the compiler will generate a dereferencing finalizer. The finalizer will effectively do the following:

  1. Iterate over each member in the object. If the

Clone this wiki locally