Boehm Garbage Collection

from Wikipedia, the free encyclopedia
Boehm Garbage Collection
Basic data

Current  version 8.0.4
( March 2, 2019 )
programming language C , C ++
category Garbage collection
License similar to MIT license
www.hboehm.info/gc

The Boehm garbage collection is a free program library for conservative garbage collection for the programming languages C and C ++ .

Details

Conservative garbage collection is used by many C or C ++ programs and in a number of runtime environments for other languages ​​such as the GNU Compiler for Java (GCJ), Portable.NET , LLVM , the GNU D Compiler and Mono (up to version 2.5).

The reference implementation supports numerous operating systems including many Unix-like ( Linux , Mac OS X , ...) and Windows and offers a number of advanced features such as incremental and parallel cleansing and a number of different finalizations . It has been ported to the D programming language with minor changes and is part of the standard runtime library Phobos of the Digital Mars D compiler. It also includes a C library for handling strings called cords. The implementation is subject to a free software license similar to the MIT license .

Garbage collection can also be used in a test mode, in which the deallocation of the memory remains manual and is checked whether it is working correctly in order to detect memory leaks and duplicate deallocations .

example

The procedure works with most unchanged C programs by simply replacing the call of malloc () with GC_malloc () and realloc () with GC_realloc () and removing free () calls. The code section below shows the use of the Boehm method instead of the conventional malloc () and free () in C. The garbage collection is based on an analysis of the stack in which the living object references are identified. This is not possible with portable C code, but requires special adaptations for each system platform.

#include <assert.h>
#include <stdio.h>
#include <gc.h>

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 = %d\n", GC_get_heap_size());
    }

    return 0;
}

Web links

Individual evidence

  1. Release 8.0.4 . March 2, 2019 (accessed March 2, 2019).
  2. Using the Garbage Collector: A simple example . ( Memento of the original from February 10, 2009 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / www.hpl.hp.com
  3. Conservative GC Porting Directions .