C++ 中的垃圾收集库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/81062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:42:34  来源:igfitidea点击:

Garbage collection Libraries in C++

c++garbage-collectionlibraries

提问by Andrew Bettison

What free and commercial garbage collection libraries are available for C++, and what are the pros and cons of each?

有哪些可用于 C++ 的免费和商业垃圾收集库,它们各自的优缺点是什么?

I am interested in hard-won lessons from actual use in the field, not marketing or promotional blurb.

我对来自该领域实际使用的来之不易的经验感兴趣,而不是营销或促销宣传。

There is no need to elaborate on the usual trade offs associated with automatic garbage collection, but please do mention the algorithms used (reference counting, mark and sweep, incremental, etc.) and briefly summarise the consequences.

无需详细说明与自动垃圾收集相关的通常权衡,但请务必提及使用的算法(引用计数、标记和清除、增量等)并简要总结结果。

回答by Greg Hewgill

I have used the Boehm collectorin the past with good success. It's open source and can be used in commercial software.

我过去使用过Boehm 收集器并取得了成功。它是开源的,可用于商业软件。

It's a conservative collector, and has a long history of development by one of the foremost researchers in garbage collection technology.

它是一个保守的收集器,并且由垃圾收集技术领域最重要的研究人员之一开发了很长的历史。

回答by Tom Leys

Boost has a great range of smart pointerswhich impliment reference counting or delete-on-scope exit or intrusive reference counting. These have proven enough for our needs. A big plus is that it is all free, open source, templated C++. because it is reference counting, in most cases it is highly deterministic when an object gets destroyed.

Boost 有很多智能指针,它们暗示引用计数或删除范围退出或侵入性引用计数。这些已经证明足以满足我们的需求。一个很大的优点是它都是免费的、开源的、模板化的 C++。因为它是引用计数,所以在大多数情况下,当一个对象被销毁时它是高度确定的。

回答by Pieter

The Boehm garbage collectoris freely available, and supposedly rather good (no first hand experience myself)

Boehm 垃圾收集器是免费提供的,据说相当不错(我自己没有第一手经验)

([PDF WARNING]Theoretical paper about C++0x proposal for the Boehm garbage collector)

([PDF 警告]关于Boehm 垃圾收集器的 C++0x 提案的理论论文)

It was originally said to make C++0x , but will not make it after all(due to time constraints I suppose).

最初据说可以制作 C++0x ,但毕竟不会制作(我想是由于时间限制)。

Proprosal N2670(minimal support for garbage collectors) did get approved in june 2008 though, so as compiler implementations pick up on this, and the standard gets finalised, the garbage collection world out there for C++ is sure to change...

不过,提案 N2670(对垃圾收集器的最低支持)确实在 2008 年 6 月获得批准,因此随着编译器实现对此有所了解,并且标准最终确定,C++ 的垃圾收集世界肯定会改变......

回答by Paul Biggar

I use boehm-gc a lot. It is straight-forward to use, but the documentation is really poor. There is a C++ page, but its quite hard to find.

我经常使用 boehm-gc。使用起来很直接,但文档真的很差。有一个 C++ 页面,但很难找到。

Basically, you just make sure that every class inherits from their base class, and that you always pass gc_allocator to a container. In a number of cases you want to use libgccpp to catch other uses of new and delete. These are largely high-level changes, and we find that we can turn off the GC at compile-time using an #ifdef, and that supporting this only affects one or two files.

基本上,您只需确保每个类都继承自它们的基类,并且始终将 gc_allocator 传递给容器。在许多情况下,您希望使用 libgccpp 来捕捉 new 和 delete 的其他用途。这些主要是高级更改,我们发现我们可以在编译时使用 #ifdef 关闭 GC,而支持此功能只会影响一两个文件。

My major problem with it is that you can no longer use Valgrind, unless you turn the collector off first. While turning the collector off is easy to do, and doesn't require recompiling, it's obviously impossible to use it if you start to run out of memory.

我的主要问题是你不能再使用 Valgrind,除非你先关闭收集器。虽然关闭收集器很容易做到,并且不需要重新编译,但如果您开始耗尽内存,显然无法使用它。

回答by larsivi

The only one I know of is Boehm, which at the bottom is a traditional mark and sweep. It probably uses various techniques to optimize this, but typically incremental/generational/compacting GC's will be hard to create for C++ without going for a managed subset such as what you can get with .Net C++. Some of the approaches that needs to move pointers can be implemented with compiler support for pinning pointers or read/write blocks though, but the effect on performance may be too big, and it isn't necessarily non-trivial changes to the GC.

我所知道的唯一一个是 Boehm,它在底部是一个传统的标记和扫描。它可能使用各种技术来优化这一点,但通常很难为 C++ 创建增量/分代/压缩 GC,而无需使用托管子集,例如您可以使用 .Net C++ 获得的子集。一些需要移动指针的方法可以通过编译器支持固定指针或读/写块来实现,但对性能的影响可能太大,并且它不一定是对 GC 的非平凡更改。

回答by Arafangion

The major difficulty with GC's in C++ is the need to handle uncooperative modules, in the GC sense. ie, to deal with libraries that were never written with GC's in mind.

在 C++ 中使用 GC 的主要困难是需要处理不合作的模块,在 GC 的意义上。即,处理从未考虑过 GC 编写的库。

This is why the Boehm GC is often suggested.

这就是为什么经常建议使用 Boehm GC 的原因。

回答by Daniel Holmes

Here's a commercial product I found in just looking for this same thing

这是我在寻找同样的东西时发现的商业产品

HnxGC http://hnxgc.harnixworld.com/

HnxGC http://hnxgc.harnixworld.com/

Back in the day, there was also a product called Great Circle from Geodesic Systems, but doesn't look like they sell that anymore. No idea if the sold the product to anyone else.

过去,Geodesic Systems 也有一款名为 Great Circle 的产品,但看起来他们不再销售了。不知道是否将产品卖给了其他人。

回答by Remi Lemarchand

You can also use Microsoft's Managed C++. The CLR and the GC are very solid and used in server products, but you have to use CLR types for the GC to actually collect - you can't just recompile your existing code and remove all the delete statements.

您还可以使用 Microsoft 的 Managed C++。CLR 和 GC 非常可靠,用于服​​务器产品,但是您必须使用 CLR 类型让 GC 实际收集 - 您不能只是重新编译现有代码并删除所有删除语句。

I would rather use C# to write brand new code, but Managed C++ lets you evolve your code base in a more progressive manner.

我宁愿使用 C# 编写全新的代码,但托管 C++ 可以让您以更渐进的方式发展您的代码库。

回答by the_drow

Read thisand take a good look at the conclusions:

阅读本文并仔细查看结论:

Conclusions

  • Complex solution to problem for which simple solutions are widely used and will be improved by C++0x leaving us little need.
  • We have little to no experience with the recommended language features which are to be standardized.
  • Fixing bad software complex system will never work.
  • Recommend minor language changes to improve future GC support - disallow hiding of pointers (xor list trick) as one example.

  • Finally - address the "C++ is bad because it has no GC" argument head-on. C++ doesn't generate garbage and so has no need for GC. Clearly Java, C#, Objective C, etc. generate lots of garbage.

结论

  • 问题的复杂解决方案,简单的解决方案被广泛使用,并且会被 C++0x 改进,而我们几乎不需要。
  • 我们对要标准化的推荐语言功能几乎没有经验。
  • 修复糟糕的软件复杂系统永远不会奏效。
  • 建议对语言进行细微的更改以改进未来的 GC 支持 - 作为一个示例,不允许隐藏指针(异或列表技巧)。

  • 最后 - 正面解决“C++ 不好,因为它没有 GC”的论点。C++ 不会产生垃圾,因此不需要 GC。显然,Java、C#、Objective C 等会产生大量垃圾。

Yes the last sentence is subjective and also a part of the holy wars.
I use C++ because I dislike the idea that someone needs to take out the garbage for me.
The city hall does that and that's enough for me.
If you need GC use another language. Pick the right tool for the right job.

是的,最后一句话是主观的,也是圣战的一部分。
我使用 C++ 是因为我不喜欢有人需要为我倒垃圾的想法。
市政厅这样做,这对我来说就足够了。
如果您需要 GC 使用另一种语言。为正确的工作选择正确的工具。