Reference counting vs. tracing garbage collection

Reference counting and tracing are two alternative approaches to garbage collection.

Language examples

Languages that use reference counting

Languages that use a tracing GC

Languages that use a combination of both

  • Jim Tcl (uses a tracing GC for refs)
  • Python (uses a tracing GC for cycle detection)

Discussion

DKF 2015-08-02: Tcl uses reference counting, and can largely get away with it because it has no reference loops. If you try to put a value inside itself (e.g., with lset or dict set) the value gets duplicated (because of copy-on-write) and the container becomes the new value. This does depend on the model-immutability of Tcl's value system though, and is the reason why we tend to be very strict on maintaining that model: merely allowing reference loops would require us to change our memory management system.

There are advantages to using reference counting, of course. In particular, it allows us to integrate with C libraries relatively easily and it is typically parsimonious in its memory use (though we cede some of that for greater thread performance). Garbage collection, while potentially faster, has greater memory overheads and higher impedance with C.

See also


AMB - 2023-09-22 14:06:06

The package vutil adds garbage collection for TclOO with traces.