Boehm garbage collector
{{Short description|Garbage collector software library}}
{{Infobox software
| name = Boehm–Demers–Weiser Garbage Collector
| logo =
| screenshot =
| caption =
| collapsible =
| other_names = bdwgc
| author = Hans-Juergen Boehm
| developer = Ivan Maidanski, et al.
| released = {{Start date and age|1988}}
| discontinued =
| latest release version = 8.2.8
| latest release date = {{Start date and age|2024|09|08}}
| latest preview version =
| latest preview date =
| operating system =
| programming language = C
| genre = garbage collector
| license = similar to X11 (free software)
| website = {{Official URL}}
}}
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++ developed by Hans Boehm, Alan Demers, and Mark Weiser.Hans Boehm, [https://hboehm.info/gc/ A garbage collector for C and C++]Andrew W. Appel (1998), [https://www.cs.princeton.edu/~appel/modern/c/ Modern Compiler Implementation in C] - "[https://www.cs.princeton.edu/~appel/modern/c/software/boehm/ Boehm Conservative Garbage Collector]",
Boehm GC is free software distributed under a permissive free software licence similar to the X11 license. The first paper introducing this collector appeared in 1992.H. J. Boehm and D. Chase, [https://grothoff.org/christian/teaching/2007/4705/boecha.pdf "A Proposal for Garbage-Collector-Safe C Compilation"], The Journal of C Language Translation, Volume 4 Number 2 December 1992, pages 126-141
Design
Hans Boehm describes the operation of the collector as follows:
{{Quotation|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.|http://www.hboehm.info/gc/#details}}
Boehm GC can also run in leak detection mode[http://www.hboehm.info/gc/leak.html Using the Garbage Collector as Leak Detector] 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 C string handling library called cords. This is similar to ropes in C++ (trees of constant small arrays), 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 to them in the middle, slicing, concatenating, and keeping history of changes (undo/redo functionality).
Operation
The garbage collector works with most unmodified C programs, simply by replacing {{tt|malloc()}} with {{tt|GC_MALLOC()}} calls, replacing {{tt|realloc()}} with {{tt|GC_REALLOC()}} calls, and removing {{tt|free()}} calls. The code piece below shows how one can use Boehm instead of traditional malloc and free in C.[http://www.hboehm.info/gc/simple_example.html Using the Garbage Collector: A simple example]
- include
- include
- include
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 {{tt|GC_FREE()}}.{{cite web |title=Garbage Collector Interface |url=https://www.hboehm.info/gc/gcinterface.html |website=www.hboehm.info}} All the substitution can be done using preprocessor macros.
Uses and ports
The Boehm GC is used by many projects[https://github.com/ivmai/bdwgc/wiki/Known-clients Known BDWGC uses] that are implemented in C or C++ like Inkscape, as well as by runtime environments for a number of other languages, including Crystal, the Codon high performance python compiler,{{cite web | url=https://github.com/exaloop/codon | title=Exaloop/Codon | website=GitHub }} the GNU Compiler for Java runtime environment, the Portable.NET project, Embeddable Common Lisp, GNU Guile, the Mono implementation of the Microsoft .NET platform (also using precise compacting GC since version 2.8), GNUstep optionally, and libgc-d[https://github.com/lycus/libgc-d libgc-d] (a binding to libgc for the D programming language, used primarily in the MCI). It supports numerous operating systems, including many Unix variants (such as macOS) and Microsoft Windows, and provides a number of advanced features including incremental collection, parallel collection and a variety of finalizer semantics.
References
{{Reflist|refs=
| last1 = Koranne
| first1 = Sandeep
| title = Handbook of Open Source Tools
| publisher = Springer
| pages = 151–154
| year = 2011
| bibcode = 2011hoos.book.....K
| isbn = 978-1441977199
| url = https://books.google.com/books?id=ukXrNh2g6fQC&pg=PA151
| postscript= .
}}
}}
External links
{{Portal|Free and open-source software}}
- {{Official website}}
- {{SourceForge|bdwgc}}
- {{GitHub|ivmai/bdwgc/|Git repo for BoehmGC development}}
- [http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2310.pdf Transparent Programmer-Directed Garbage Collection for C++, Hans-J. Boehm and Michael Spertus]
- [http://www.drdobbs.com/the-boehm-collector-for-c-and-c/184401632 Dr. Dobbs The Boehm Collector for C and C++, Gene Michael Stover, March 01, 2003]
{{Memory management}}
Category:Automatic memory management