Boehm garbage collector
Encyclopedia
In computer science
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...

, the Boehm-Demers-Weiser garbage collector, often simply known as Boehm GC, is a conservative garbage collector
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

 for C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

.

Boehm GC is free software
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...

 distributed under a permissive free software licence
Free software licence
A free software licence is a software licence which grants recipients rights to modify and redistribute the software, which would otherwise be prohibited by copyright law. A free software licence grants, to the recipients, freedoms in the form of permissions to modify or distribute copyrighted work...

 similar to the X11 license.

Design

The developer 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 test mode 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
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....

 handling library called cords
CORDS
The acronym CORDS may refer to:* Civil Operations and Revolutionary Development Support, a pacification program of the U.S. in the Vietnam War* Cords Cable Industries Limited, an India based multinational corporationCords may also refer to:...

. This is similar to ropes
Rope (computer science)
In computer programming a rope, or cord, is a data structure for efficiently storing and manipulating a very long string. For example, a text editing program may use a rope to represent the text being edited, so that operations such as insertion, deletion, and random access can be done...

 in C++ (strings are tree
Tree
A tree is a perennial woody plant. It is most often defined as a woody plant that has many secondary branches supported clear of the ground on a single main stem or trunk with clear apical dominance. A minimum height specification at maturity is cited by some authors, varying from 3 m to...

s 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 to them in the middle, slicing, concatenating, and keeping history of changes (undo
Undo
Undo is a command in many computer programs. It erases the last change done to the document reverting it to an older state. In some more advanced programs such as graphic processing, undo will negate the last command done to the file being edited....

/redo functionality).

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. The code piece below shows how one can use Boehm instead of traditional malloc
Malloc
C dynamic memory allocation refers to performing dynamic memory allocation in the C via a group of functions in the C standard library, namely malloc, realloc, calloc and free....

 and free in C. An annotated version passing Splint memory leak checks is available at GitHub.
  1. include
  2. include
  3. include


int main(void)
{
int i;

GC_INIT;
for (i = 0; i < 10000000; ++i)
{
int **p = (int**)GC_MALLOC(sizeof(int *));
int *q = (int*)GC_MALLOC_ATOMIC(sizeof(int));

assert(*p

0);
*p = (int*)GC_REALLOC(q, 2 * sizeof(int));
if (i % 100000

0)
printf("Heap size = %zu\n", GC_get_heap_size);
}

return 0;
}

Uses and ports

The Boehm GC 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
Portable.NET
Part of the DotGNU project, Portable.NET is a free software and open source software initiative aiming to build a portable toolchain and runtime for Common Language Infrastructure applications. The project focuses on compatibility with the ECMA-334 and ECMA-335 standards and support for .NET's...

 project, LLVM, GNU D Compiler
D (programming language)
The D programming language is an object-oriented, imperative, multi-paradigm, system programming language created by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is mainly influenced by that language, it is not a variant of C++...

, Embeddable Common Lisp
Embeddable Common Lisp
Embeddable Common Lisp is a LGPL Common Lisp implementation aimed at producing a small-footprint Lisp system that can be embedded into existing C-based applications...

 and the Mono
Mono (software)
Mono, pronounced , is a free and open source project led by Xamarin to create an Ecma standard compliant .NET-compatible set of tools including, among others, a C# compiler and a Common Language Runtime....

 implementation of the Microsoft .NET platform (the last is also using precise compacting GC since version 2.8). It supports numerous operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

s, including many Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 variants (such as Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

) and Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

, and provides a number of advanced features including incremental collection, parallel collection and a variety of finalizer
Finalizer
In object-oriented programming languages that use garbage collection, a finalizer is a special method that is executed when an object is garbage collected. It is similar in function to a destructor...

semantics. Boehm GC was ported with small changes to the 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 an unmodified C version).

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK