Boehm garbage collector
From Wikipedia, the free encyclopedia
| Written in | C and C++ |
|---|---|
| Type | garbage collector |
| License | free software licence similar to X11 |
| Website | http://www.hpl.hp.com/personal/Hans_Boehm/gc/ |
In computer science, the Boehm-Demers-Weiser garbage collector, often simply known as Boehm GC, is a conservative garbage collector for C and C++, which is used by many projects that are implemented in C or C++, as well as by runtime environments for a number of other languages, including the GNU Compiler for Java runtime environment, the Portable.NET project, LLVM, GNU D Compiler and the Mono implementation of the Microsoft .NET platform (the last is going to change garbage collector to precise compacting GC in version 2.6). It supports numerous operating systems, including many Unix variants (such as Mac OS X) and Microsoft Windows, and provides a number of advanced features including incremental collection, parallel collection and a variety of finalizer semantics. Boehm GC was ported with small changes to D programming language and is part of Digital Mars D compiler's standard runtime library called Phobos (this differs to other usage, because other runtimes use unmodified C version).
Boehm GC can also run in test mode in which memory management still is done manually, but Boehm GC can check if it is done properly. This way programmer can find memory leaks and double deallocations.
Boehm GC is also distributed with C string handling library called cords. This is similar to ropes in C++ - strings are trees of small arrays, and they never change - but instead of using reference counting for proper deallocation, it relies on garbage collection to free objects. Cords are good at handling very large texts, modifications of them in the middle, slicing, concatenating, and keeping history of changes (undo/redo functionality).
Boehm GC is free software distributed under a permissive free software licence similar to that of X11.
[edit] Example
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. The code piece below shows how one can use Boehm instead of traditional malloc and free in C. [1]
#include <assert.h> #include <stdio.h> #include <gc.h> int main(void) { int i; GC_INIT(); for (i = 0; i < 10000000; ++i) { int **p = GC_MALLOC(sizeof(int *)); int *q = GC_MALLOC_ATOMIC(sizeof(int)); assert(*p == 0); *p = GC_REALLOC(q, 2 * sizeof(int)); if (i % 100000 == 0) printf("Heap size = %d\n", GC_get_heap_size()); } return 0; }
[edit] References
[edit] External links
- Homepage
- Implementation overview
- Sourceforge website
- Transparent Programmer-Directed Garbage Collection for C++, Hans-J. Boehm and Michael Spertus
- Using the C/C++ Garbage Collection Library

