垃圾收集器作用於未變性的(unmodified)C程式,只要簡單的將malloc呼叫用GC_malloc取代,將realloc取代為GC_realloc呼叫,如此一來便不需要使用到free的函式。下列的程式碼展示出如何用Boehm取代傳統的malloc以及free。[1].
#include <assert.h>
#include <stdio.h>
#include <gc.h>
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;
}