The Boehm-Demers-Weiser conservative C/C++ Garbage Collector (bdwgc, also known as bdw-gc, boehm-gc, libgc)
The Boehm-Demers-Weiser conservative C/C++ Garbage Collector (bdwgc, also known as bdw-gc, boehm-gc, libgc)
This is version 8.3.0 (next release development) of a conservative garbage collector for C and C++.
License: MIT-style
You might find a more recent/stable version on the Download page, or BDWGC site.
Also, the latest bug fixes and new features are available in the development repository.
This is intended to be a general purpose, garbage collecting storage allocator. The algorithms used are described in:
Boehm, H., and M. Weiser, "Garbage Collection in an Uncooperative Environment", Software Practice & Experience, September 1988, pp. 807-820.
Boehm, H., A. Demers, and S. Shenker, "Mostly Parallel Garbage Collection", Proceedings of the ACM SIGPLAN '91 Conference on Programming Language Design and Implementation, SIGPLAN Notices 26, 6 (June 1991), pp. 157-164.
Boehm, H., "Space Efficient Conservative Garbage Collection", Proceedings of the ACM SIGPLAN '91 Conference on Programming Language Design and Implementation, SIGPLAN Notices 28, 6 (June 1993), pp. 197-206.
Boehm H., "Reducing Garbage Collector Cache Misses", Proceedings of the 2000 International Symposium on Memory Management.
Possible interactions between the collector and optimizing compilers are discussed in
Boehm, H., and D. Chase, "A Proposal for GC-safe C Compilation", The Journal of C Language Translation 4, 2 (December 1992).
Boehm H., "Simple GC-safe Compilation", Proceedings of the ACM SIGPLAN '96 Conference on Programming Language Design and Implementation.
Unlike the collector described in the second reference, this collector operates either with the mutator stopped during the entire collection (default) or incrementally during allocations. (The latter is supported on fewer machines.) On the most common platforms, it can be built with or without multi-threading support. On some platforms, it can take advantage of a multiprocessor to speed up garbage collection.
Many of the ideas underlying the collector have previously been explored by others. Notably, some of the run-time systems developed at Xerox PARC in the early 1980s conservatively scanned thread stacks to locate possible pointers (cf. Paul Rovner, "On Adding Garbage Collection and Runtime Types to a Strongly-Typed Statically Checked, Concurrent Language" Xerox PARC CSL 84-7). Doug McIlroy wrote a simpler fully conservative collector that was part of version 8 UNIX (tm), but appears to not have received widespread use.
Rudimentary tools for use of the collector as a leak detector are included, as is a fairly sophisticated string package "cord" that makes use of the collector. (See cords.md and H.-J. Boehm, R. Atkinson, and M. Plass, "Ropes: An Alternative to Strings", Software Practice and Experience 25, 12 (December 1995), pp. 1315-1330. This is very similar to the "rope" package in Xerox Cedar, or the "rope" package in the SGI STL or the g++ distribution.)
Further collector documentation can be found in the overview.
Some of the known uses of the collector are listed on the GitHub Known-clients page.
This is a garbage collecting storage allocator that is intended to be used as a plug-in replacement for C's malloc.
Since the collector does not require pointers to be tagged, it does not attempt to ensure that all inaccessible storage is reclaimed. However, in our experience, it is typically more successful at reclaiming unused memory than most C programs using explicit deallocation. Unlike manually introduced leaks, the amount of unreclaimed memory typically stays bounded.
In the following, an "object" is defined to be a region of memory allocated by the routines described below.
Any objects not intended to be collected must be pointed to either
from other such accessible objects, or from the registers,
stack, data, or statically allocated bss segments. Pointers from
the stack or registers may point to anywhere inside an object.
The same is true for heap pointers if the collector is compiled with
ALL_INTERIOR_POINTERS defined, or GC_all_interior_pointers is otherwise
set, as is now the default.
Compiling without ALL_INTERIOR_POINTERS may reduce accidental retention
of garbage objects, by requiring pointers from the heap to the beginning
of an object. But this no longer appears to be a significant
issue for most programs occupying a small fraction of the possible
address space.
There are a number of routines which modify the pointer recognition
algorithm. GC_register_displacement allows certain interior pointers
to be recognized even if ALL_INTERIOR_POINTERS is not defined.
GC_malloc_ignore_off_page allows some pointers into the middle of
large objects to be disregarded, greatly reducing the probability of
accidental retention of large objects. For most purposes it seems
best to compile with ALL_INTERIOR_POINTERS and to use
GC_malloc_ignore_off_page if you get collector warnings from
allocations of very large objects. See the
debugging documentation for details.
Warning: pointers inside memory allocated by the standard (system) malloc
are not seen by the garbage collector. Thus objects pointed to only from such
a region may be prematurely deallocated. It is thus suggested that the
standard malloc be used only for memory regions, such as I/O buffers, that
are guaranteed not to contain pointers to garbage collectible memory.
Pointers in C language automatic, static, or register variables,
are correctly recognized. (Note that GC_malloc_uncollectable has
semantics similar to standard malloc, but allocates objects that are
traced by the collector.)
Warning: the collector does not always know how to find pointers in data
areas that are associated with dynamic libraries. This is easy to remedy
if you know how to find those data areas on your operating system (see
GC_add_roots). Code for doing this under SunOS, Irix 5.x and 6.x, HP/UX,
Alpha OSF/1 (Tru64 UNIX), Linux, and Win32 is included and used by default.
(See README.win32 and
README.win64 for Windows details.) On other
systems, pointers from dynamic library data areas may not be considered by the
collector. If you are writing a program that depends on the collector
scanning dynamic library data areas, it may be a good idea to include at least
one call to GC_is_visible to ensure that those areas are visible to the
collector.
Note that the garbage collector does not need to be informed of shared read-only data. However, if the shared library mechanism can introduce discontiguous data areas that may contain pointers then the collector does need to be informed.
Signal processing for most signals may be deferred during collection, and during uninterruptible parts of the allocation process. Like standard ANSI C mallocs, by default it is unsafe to invoke malloc (and other GC routines) from a signal handler while another malloc call may be in progress.
The allocator/collector can also be configured for thread-safe operation. (Full signal safety can also be achieved, but only at the cost of two system calls per malloc, which is usually unacceptable.)
Warning: the collector does not guarantee to scan thread-local storage
(e.g. of the kind accessed with pthread_getspecific). The collector
does scan thread stacks, though, so generally the best solution is to
ensure that any pointers stored in thread-local storage are also
stored on the thread's stack for the duration of their lifetime.
(This is arguably a longstanding bug, but it has not been fixed yet.)
There are multiple ways to build the collector:
The simplest way to build gc library (as well as cord library) and run
the tests using CMake:
mkdir build
cd build
cmake ..
cmake --build .
ctest
This is the most cross-platform way of building the library. See cmake.md for details.
Please note that the collector source repository does not contain configure
and similar auto-generated files, thus the full procedure of autoconf-based
build of the collector from the source repository could look like:
./autogen.sh
./configure
make check
The GNU style build process understands the usual targets and options.
make install installs gc and cord libraries. Try ./configure --help
to see all the configuration options. It is currently not possible to
exercise all combinations of build options this way.
See autoconf.md for details.
Building and testing the collector using zig is straight forward in its simplest form:
zig build test
It is possible to configure the build through the use of variables, e.g.
zig build -Denable_redirect_malloc -Denable_threads=false. Zig offers
excellent cross-compilation functionality, it is configurable like this:
zig build -Dtarget=riscv64-linux-musl
The appropriate Zig binary package file could be downloaded from the official Zig releases page.
On Windows, assuming the Microsoft build tools are installed and suitably
configured, it is possible to build the library and run the tests using
nmake directly, e.g. by by typing nmake -f NT_MAKEFILE check. However,
the recommended way is to use cmake as described above.
See README.win32 for details.
For the old-style (classic) makefile-based build process, typing
make -f Makefile.direct check will automatically build gc, cord
libraries, then run a number of tests such as gctest. The test is
a somewhat superficial test of collector functionality. Failure is indicated
by a core dump or a message to the effect that the collector is broken.
gctest may take a dozen of seconds to run on reasonable 2023 vintage 64-bit
desktops. It may use up to about 30 MB of memory.
Makefile.direct file generates a libgc.a file which you should link
against.
Finally, on most targets, the collector could be built and tested directly with a single compiler invocation, like this (the sample lacks multi-threading support):
cc -I include -o gctest tests/gctest.c extra/gc.c && ./gctest
E.g., this could be convenient for a debugging purpose.
The library can be configured more precisely during the build by defining the macros listed in macros.md file.
The library is built with multi-threading support enabled (i.e. for thread-safe operation) by default, unless explicitly disabled by:
-Denable_threads=false option passed to cmake or zig build--disable-threads option passed to ./configureThe collector operates silently in the default configuration.
In the event of issues, this can usually be changed by defining the
GC_PRINT_STATS or GC_PRINT_VERBOSE_STATS environment variables. This
will result in a few lines of descriptive output for each collection.
(The given statistics exhibit a few peculiarities.
Things do not appear to add up for a variety of reasons, most notably
fragmentation losses. These are probably much more significant for the
contrived program gctest than for your application.)
Use (cloning) of libatomic_ops is now optional provided the compiler
supports atomic intrinsics. Most modern compilers do. The notable exception
is the MS compiler (as of Visual Studio 2022).
If needed, most OS distributes have libatomic_ops package; alternati
No open issues yet, or sync has not completed.