[16.27] What are the two kinds of garbage collectors for C++?
In general, there seem to be two flavors of garbage collectors for C++:
-
Conservative garbage collectors. These know little or nothing about
the layout of the stack or of C++ objects, and simply look for bit patterns
that appear to be pointers. In practice they seem to work with both C and C++
code, particularly when the average object size is small. Here are some
examples, in alphabetical order:
-
Hybrid garbage collectors. These usually scan the stack
conservatively, but require the programmer to supply layout information for
heap objects. This requires more work on the programmer's part, but may
result in improved performance. Here are some examples, in alphabetical
order:
Since garbage collectors for C++ are normally conservative, they can sometimes
leak if a bit pattern "looks like" it might be a pointer to an otherwise
unused block. Also they sometimes get confused when pointers to a block
actually point outside the block's extent (which is illegal, but some
programmers simply must push the envelope; sigh) and (rarely) when a
pointer is hidden by a compiler optimization. In practice these problems are
not usually serious, however providing the collector with hints about the
layout of the objects can sometimes ameliorate these issues.