我尝试在这个简单的代码中使用libgc (BDW垃圾收集器)。
请注意,引用只包含假“列表”中的最后一个节点,因此,活动集合只有两个最后一个节点。
// 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;
}
}但它不能停留在内存限制内:
$ 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我做错了什么?Ubuntu 15.04 x86_64 gcc 4.9.2 libgc 7.2d-6.4
更新:我刚刚从https://github.com/ivmai/bdwgc编译了主干版本,它看起来工作正常。因此,bug仅存在于7.2d或打包到Ubuntu的版本中。
更新:从源码编译的libgc 7.2f也可以正常工作。所以这只是Ubuntu和Debian的版本问题。
发布于 2015-07-15 06:54:00
它可能只是一个bug,但也可能是假指针的受害者。BDWGC是一种保守的GC;如果一个“看起来像”指针的单词恰好指向一个GC_malloced内存,那么内存就会被保留。如果某个假指针碰巧指向您的列表节点之一,它将被意外保留,并且从它指向的所有节点也将被保留。
从弱GC健壮性的角度对其进行了讨论。有关详细信息,请参阅以下文章:
http://www.hpl.hp.com/techreports/2001/HPL-2001-251.pdf
一种常见的习惯用法是,当节点不使用时,手动使下一个链接无效。
发布于 2015-07-15 02:45:26
因为您是在无限循环中分配内存。
https://stackoverflow.com/questions/31380641
复制相似问题