Boehm garbage collector
Template:Short description {{ safesubst:#invoke:Unsubst||date=__DATE__ |$B= Template:AmboxTemplate:DMCA }} {{#invoke:Infobox|infobox}}Template:Template other {{#invoke:Check for unknown parameters|check|unknown=Template:Main other|preview=Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y| AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo class | logo size | logo title | logo upright | logo_alt | logo_caption | logo_class | logo_size | logo_title | logo_upright | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | qid | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot class | screenshot size | screenshot title | screenshot upright | screenshot_alt | screenshot_class | screenshot_size | screenshot_upright | service_name | size | standard | title | ver layout | website }}Template:Main other{{#invoke:Check for conflicting parameters|check | template = Template:Infobox software | cat = Template:Main other | logo size; logo_size | logo class; logo_class | logo alt; logo_alt | logo title; logo_title | logo caption; logo_caption | screenshot size; screenshot_size | screenshot upright; screenshot_upright | screenshot class; screenshot_class | screenshot alt; screenshot_alt | screenshot title; screenshot title | latest release version; latest_release_version | latest release date; latest_release_date | latest preview version; latest_preview_version | latest preview date; latest_preview_date | latest release version; latest_release_version | latest release date; latest_release_date | latest preview version; latest_preview_version | latest preview date; latest_preview_date | programming language; programming_language | operating system; operating_system }}
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 free software distributed under a permissive free software licence similar to the X11 license. The first paper introducing this collector appeared in 1992.[4]
Design
Hans Boehm describes the operation of the collector as follows:[5]
The collector uses a mark-sweep algorithm. It provides incremental and generational collection under operating systems that provide the right kind of virtual memory support. (CurrentlyTemplate:When this includes Linux, *BSD, recent Windows versions, MacOS X, HP/UX, Solaris, Tru64, Irix, and a few other operating systems, 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[6] 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 <syntaxhighlight lang="text" class="" style="" inline="1">malloc()</syntaxhighlight> with <syntaxhighlight lang="text" class="" style="" inline="1">GC_MALLOC()</syntaxhighlight> calls, replacing <syntaxhighlight lang="text" class="" style="" inline="1">realloc()</syntaxhighlight> with <syntaxhighlight lang="text" class="" style="" inline="1">GC_REALLOC()</syntaxhighlight> calls, and removing <syntaxhighlight lang="text" class="" style="" inline="1">free()</syntaxhighlight> calls.[1] The garbage collection features can be accessed from a header <gc.h>. The code piece below shows how one can use Boehm instead of traditional [[malloc|Template:Mono]] and Template:Mono in C.[7]
<syntaxhighlight lang="c">
- include <assert.h>
- include <stdio.h>
- include <stdlib.h>
- include <gc.h>
int main(int argc, char* argv[]) {
const int SIZE = 10000000;
GC_INIT();
for (int i = 0; i < SIZE; ++i) {
int** p = GC_MALLOC(sizeof(*p));
int* q = GC_MALLOC_ATOMIC(sizeof(*q));
assert(*p == NULL);
*p = GC_REALLOC(q, 2 * sizeof(*p));
if (i == SIZE - 1) {
printf("Heap size = %zu\n", GC_get_heap_size());
}
}
return 0;
} </syntaxhighlight>
For completeness, Boehm supports explicit deallocation via <syntaxhighlight lang="text" class="" style="" inline="1">GC_FREE()</syntaxhighlight>.[8] All the substitution can be done using preprocessor macros.
Uses and ports
The Boehm GC is used by many projects[9] 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,[10] 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[11] (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
External links
{{#invoke:Portal|portal}}
- Template:Official website
- Template:GitHub
- Template:SourceForge
- Transparent Programmer-Directed Garbage Collection for C++, Hans-J. Boehm and Michael Spertus
- Dr. Dobbs The Boehm Collector for C and C++, Gene Michael Stover, March 01, 2003
- ↑ 1.0 1.1 Cite error: Invalid
<ref>tag; no text was provided for refs namedKoranne2011 - ↑ Hans Boehm, A garbage collector for C and C++ Template:Webarchive
- ↑ Andrew W. Appel (1998), Modern Compiler Implementation in C Template:Webarchive - "Boehm Conservative Garbage Collector Template:Webarchive",
- ↑ 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
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ Using the Garbage Collector: A simple example
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}