tar xvfz gc<version>.tar.gz cd gc<version>
You can configure, build, and install the collector in a private directory, say /home/xyz/gc, with the following commands:
./configure --prefix=/home/xyz/gc --disable-threads make make check make installHere the "make check" command is optional, but highly recommended. It runs a basic correctness test which usually takes well under a minute.
If you use a makefile that does not require running a configure script, you should first look at the makefile, and adjust any options that are documented there.
If your platform provides a "make" utility, that is generally preferred to platform- and compiler- dependent "project" files. (At least that is the strong preference of the would-be maintainer of those project files.)
--enable-threads=posix --enable-thread-local-alloc --enable-parallel-markinstead of --disable-threads If your target is a real old-fashioned uniprocessor (no "hyperthreading", etc.) you will want to omit --enable-parallel-mark.
#include "gc.h"at the beginning of every file that allocates memory through the garbage collector. Call GC_MALLOC wherever you would have call malloc. This initializes memory to zero like calloc; there is no need to explicitly clear the result.
If you know that an object will not contain pointers to the garbage-collected heap, and you don't need it to be initialized, call GC_MALLOC_ATOMIC instead.
A function GC_FREE is provided but need not be called. For very small objects, your program will probably perform better if you do not call it, and let the collector do its job.
A GC_REALLOC function behaves like the C library realloc. It allocates uninitialized pointer-free memory if the original object was allocated that way.
The following program loop.c is a trivial example:
#include "gc.h" #include <assert.h> #include <stdio.h> int main() { int i; GC_INIT(); /* Optional on Linux/X86; see below. */ 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; }
#define GC_REDIRECT_TO_LOCAL #include "gc_local_alloc.h"This will cause GC_MALLOC and GC_MALLOC_ATOMIC to keep per-thread allocation caches, and greatly reduce the number of lock acquisitions during allocation. For versions after 7.0, this happens implicitly if the collector is built with thread-local allocation enabled.
cc -I/home/xyz/gc/include loop.c /home/xyz/gc/lib/libgc.a -o loopThe -I option directs the compiler to the right include directory. In this case, we list the static library directly on the compile line; the dynamic library could have been used instead, provided we arranged for the dynamic loader to find it, e.g. by setting LD_LIBRARY_PATH.
./loopThe operation of the collector is affected by a number of environment variables. For example, setting GC_PRINT_STATS produces some GC statistics on stdout. See README.environment in the distribution for details.