Boehm GC 如何用于 C 程序?
我检查了 Boehm GC。 C/C++ 的 GC。
我知道标记和清除算法。我很好奇的是它如何只获取整个 C 内存中的指针。我对C内存的理解只是一个普通的字节数组。是否可以确定内存中的值是否为指针?
I checked Boehm GC. The GC for C/C++.
I know mark-and-sweep algorithm. What I'm in curious is how it picks up only pointers in whole C memory. My understanding about C memory is just a plain byte array. Is it possible to determine a value in memory is pointer or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Boehm GC 是一个保守的收集器,这意味着它假设所有内容(在指针边界上)都是指针。这意味着它可以找到误报引用,例如恰好具有堆中地址值的整数。因此,某些块在内存中的停留时间可能比使用非保守收集器时要长。
以下是 Boehm 页面 的描述:
您还应该知道,需要为 Boehm GC 提供一组“根”,它们是标记和清除算法的起点。堆栈和寄存器自动成为根。您需要显式添加全局指针作为根。
编辑:在评论中,人们指出了对保守派收藏家的一些担忧。确实,看起来像指向收集器的堆指针的整数可能会导致内存无法释放。这并不像您想象的那么大问题。程序中的大多数标量整数用于计数和大小,并且相当小(因此它们看起来不像堆指针)。您通常会遇到包含位图、字符串、浮点数据或任何此类数据的数组的问题。 Boehm GC 允许您使用 GC_MALLOC_ATOMIC 分配块,这向收集器指示该块将不包含任何指针。如果您查看 gc_typed.h,您还会找到指定哪些部分的方法块可以包含指针。
也就是说,保守收集器的一个基本限制是它无法在收集期间安全地移动内存,因为指针重写不安全。这意味着您将无法获得压缩的任何好处,例如减少碎片和提高缓存性能。
The Boehm GC is a conservative collector, which means it assumes everything (on pointer boundaries) is a pointer. This means that it can find false positive references, like an integer which coincidentally has the value of an address in the heap. As a result, some blocks may stay in memory longer than they would with a non-conservative collector.
Here's a description from Boehm's page:
You should also know that the Boehm GC needs to be given a set of "roots", which are starting points for the mark-and-sweep algorithm. The stack and registers are automatically roots. You need to explicitly add global pointers as roots.
EDIT: In comments, some concerns were pointed out about conservative collectors in general. It is true that integers that look like heap pointers to the collector can cause memory not to be released. This is not as big of a problem as you might think. Most scalar integers in a program are used for counts and sizes and are fairly small (so they would not look like heap pointers). You would mostly run into problems with arrays containing bitmaps, strings, floating point data, or anything of that sort. Boehm GC lets you allocate a block with
GC_MALLOC_ATOMIC
which indicates to the collector that the block will not contain any pointers. If you look in gc_typed.h, you will also find ways to specify what parts of a block may contain pointers.That said, a fundamental limitation of a conservative collector is that it cannot safely move memory around during collection since pointer rewriting is not safe. This means you won't get any of the benefits of compaction like lowered fragmentation and improved cache performance.