Boehm garbage collector
Other names | bdwgc |
---|---|
Original author(s) | Hans-Juergen Boehm |
Developer(s) | Ivan Maidanski, et al. |
Initial release | 1988 |
Stable release | 8.2.4
/ May 26, 2023 |
Repository | |
Written in | C |
Type | garbage collector |
License | similar to X11 (free software) |
Website | www |
The Boehm–Demers–Weiser garbage collector, often simply known as the Boehm GC or Boehm collector, is a conservative garbage collector for C and C++[1] developed by Hans Boehm, Alan Demers, and Mark Weiser.[2][3]
Boehm GC is
Design
Hans Boehm describes the operation of the collector as follows:
The collector uses a mark-sweep algorithm. It provides incremental and generational collection under operating systems which provide the right kind of virtual memory support. (Currently this includes SunOS[45], IRIX, OSF/1, Linux, and Windows, with varying restrictions.) It allows finalization code to be invoked when an object is collected. It can take advantage of type information to locate pointers if such information is provided, but it is usually used without such information.
Boehm GC can also run in leak detection mode[5] in which memory management is still done manually, but the Boehm GC can check if it is done properly. In this way a programmer can find memory leaks and double deallocations.
Boehm GC is also distributed with a
Operation
The garbage collector works with most unmodified C programs, simply by replacing malloc() with GC_MALLOC() calls, replacing realloc() with GC_REALLOC() calls, and removing free() calls.
#include <assert.h>
#include <stdio.h>
#include <gc.h>
int main(void)
{
int i;
const int size = 10000000;
GC_INIT();
for (i = 0; i < size; ++i)
{
int **p = GC_MALLOC(sizeof *p);
int *q = GC_MALLOC_ATOMIC(sizeof *q);
assert(*p == 0);
*p = GC_REALLOC(q, 2 * sizeof *p);
if (i == size-1)
printf("Heap size = %zu\n", GC_get_heap_size());
}
return 0;
}
For completeness, Boehm supports explicit deallocation via GC_FREE().[7] All the substitution can be done using preprocessor macros.
Uses and ports
The Boehm GC is used by many projects
References
- ^ ISBN 978-1441977199.
- ^ Hans Boehm, A garbage collector for C and C++
- Andrew W. Appel (1998), Modern Compiler Implementation in C - "Boehm Conservative Garbage Collector",
- ^ H. J. Boehm and D. Chase, "A Proposal for Garbage-Collector-Safe C Compilation", The Journal of C Language Translation, Volume 4 Number 2 December 1992, pages 126-141
- ^ Using the Garbage Collector as Leak Detector
- ^ Using the Garbage Collector: A simple example
- ^ "Garbage Collector Interface". www.hboehm.info.
- ^ Known BDWGC uses
- ^ "Exaloop/Codon". GitHub.
- ^ libgc-d