I've tried to use libgc (BDW garbage collector) within this simple code.
Note, that reference holded only to last node in a fake "list", so that, living set is only two last nodes.
// thanks to @chill for this example
#include <gc.h>
struct list {
struct list* next;
};
int main() {
GC_INIT();
struct list *last = NULL;
for (;;) {
struct list* nuo = GC_MALLOC(sizeof(struct list));
nuo->next = NULL;
// if next line is commented, then no leakage
if (last) last->next = nuo;
last = nuo;
}
}
But it could not stay within memory limits:
$ gcc -O0 gc.c -lgc -o gc
$ GC_MAXIMUM_HEAP_SIZE=100000000 ./gc
GC Warning: Out of Memory! Trying to continue ...
GC Warning: Out of Memory! Trying to continue ...
GC Warning: Out of Memory! Trying to continue ...
GC Warning: Out of Memory! Trying to continue ...
GC Warning: Out of Memory! Heap size: 95 MiB. Returning NULL!
Segmentation fault
What do I do wrong? Ubuntu 15.04 x86_64 gcc 4.9.2 libgc 7.2d-6.4
Update: i've just compiled trunk version from https://github.com/ivmai/bdwgc and it looks to work properly. So that, bug is only in 7.2d or in a version packaged to Ubuntu.
Update: libgc 7.2f compilled from source also works properly. So that it is just Ubuntu's and Debian's version issue.