- Boehm garbage collector
In
computer science , the Boehm-Demers-Weiser garbage collector, often simply known as Boehm GC, is a conservative garbage collector for C andC++ , 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 theGNU Compiler for Java runtime environment, thePortable.NET project, and the Mono implementation of theMicrosoft .NET platform. It supports numerousoperating system s, including manyUnix variants (such asMac OS X ) andMicrosoft Windows , and provides a number of advanced features including incremental collection, parallel collection and a variety offinalizer semantics.Boehm GC is
free software distributed under a permissivefree software licence similar to that ofX11 .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. [ [http://www.hpl.hp.com/personal/Hans_Boehm/gc/simple_example.html Using the Garbage Collector: A simple example ] ]#include
#include #include 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 ", GC_get_heap_size()); } return 0; } References
External links
* [http://www.hpl.hp.com/personal/Hans_Boehm/gc/ Homepage]
* [http://www.hpl.hp.com/personal/Hans_Boehm/gc/gcdescr.html Implementation overview]
* [http://sourceforge.net/projects/bdwgc/ Sourceforge website]
Wikimedia Foundation. 2010.