Boehm garbage collector
From Wikipedia, the free encyclopedia
| Créateur | Hans-J. Boehm, Alan J. Demers, Mark Weiser |
|---|---|
| Première version | |
| Dernière version | 8.2.10 () |
| Dépôt | https://github.com/ivmai/bdwgc |
| Écrit en | C, C++ |
| Supporte les langages | C, C++ |
| Type | Ramasse-miettes |
| Licence | Licence MIT et licence publique générale GNU |
| Site web | https://www.hboehm.info/gc/ |
Le garbage collector Boehm – Demers – Weiser, souvent appelé Boehm GC, est un ramasse-miettes (garbage collector en anglais) conservateur pour C et C ++ développé par Hans Boehm, Alan Demers et Mark Weiser[1],[2].
Boehm GC est un logiciel libre distribué sous une licence permissive similaire à la licence X11.
Son fonctionnement est décrit comme suit par Hans Boehm :
« 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. »
Le Boehm GC fonctionne également en mode détection de fuite[3]. Dans ce mode, la mémoire est toujours gérée manuellement, mais Boehm GC fait des vérifications afin de mieux cerner les fuites mémoires ainsi que les désallocations multiples.
Opération
Ce ramasse-miettes fonctionne avec la plupart des programmes C sans nécessiter de modifications majeures. Il suffit de remplacer malloc() par GC_MALLOC(), realloc() par GC_REALLOC() et supprimer les appels à free()[4]. L'exemple suivant montre un cas d'utilisation[5].
#include <assert.h>
#include <stdio.h>
#include <gc.h>
int main(void)
{
int i;
const 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;
}