3rd-party垃圾收集器使用的方法

发布于 2024-08-23 06:12:58 字数 349 浏览 7 评论 0原文

我写信是为了澄清该网站上的一些评论。

1)我知道C++没有垃圾收集器。有人说 C++ 是在垃圾收集器的想法出现之前发明的,所以就是这个原因。这是真的吗?我认为这是有道理的。

2)每当讨论垃圾收集器时,都会提出智能点(例如boost::share_ptr)作为一种方式。我曾经坚信引用计数是实现垃圾收集器的一种方法,但有人说,智能点并不是垃圾收集器的实现。到底是怎么回事?

3)有人说为什么C++中没有包含垃圾收集器是因为它很难并且很多问题无法解决。然而,有人说有第三方垃圾收集器可用,无论是商业的还是免费的。那么这些第三方是如何解决这些问题的呢?

如果有人能澄清我的困惑,我将不胜感激。

非常感谢!

I am writing to clarify some comments on this website.

1) I know that C++ has no garbage collector. One said that C++ was invented before the idea of garbage collector, so that's the reason. Is that true? I think it makes sense.

2) Whenever garbage collector was discussed, smart point(such as boost::share_ptr) was brought out to be a way. I was once convinced that reference counting is one way to implement garbage collector, but some said, smart point is not an implementation of garbage collector. What's the case?

3) Some said why garbage collector was not included in C++ is because it was hard and a lot of problem couldn't be solved. However, somebody else said that there were 3rd-party garbage collector were available, no matter it is commercial or for-free. So how does these 3rd-party deal with the problems?

I am grateful if anybody could clarify my confusions.

Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅紫色的梦幻 2024-08-30 06:12:58
  1. 不,垃圾收集比 C++ 古老得多(特别是许多 Lisp 版本在 60 年代就已经有了它)。

    不,

  2. 引用计数是实现垃圾收集的一种方法,但它的性能相当差(新的 Unladen Swallow 项目,为了加速 CPython 解释器,包括从引用计数转向更好的垃圾收集实现——显着的性能提升)。

  3. C 和 C++ 的 Boehm 收集器使用 保守方法:简单来说,任何看起来像地址的东西都被视为地址(因此不会收集它可能“指向”的任何内容)。请阅读我提供的 URL 上的页面及其传出链接,以获取有关该主题的更多信息。

  1. no, garbage collection is far older than C++ (many Lisp versions had it in the '60s, in particular).

  2. reference counting is a way to implement garbage collection, but it's quite poor performance-wise (the new Unladen Swallow project, to accelerate the CPython interpreter, includes moving from reference counting to a better garbage collection implementation -- a substantial performance boost).

  3. the Boehm collector for C and C++ uses a conservative approach: briefly, anything that looks like an address is taken to be one (so whatever it might "point to" is not collected). Read the page at the URL I've given, and its outgoing links, for much more information on the subject.

迟到的我 2024-08-30 06:12:58
  1. 正如 Alex 指出的那样,早在 C++ 发明之前,LISP 就已经有了垃圾收集功能。 OTOH,其中一些早期实现使用了引用计数。

  2. 大多数讨论垃圾收集的人都会想到一些隐藏的东西。引用计数确实存在许多问题。它可能存在性能问题,但由于对象通常在 C++ 中使用,因此很少出现性能问题。一个更大的问题是引用计数通常不能很好地处理数据中的循环。对于一个简单的例子,请考虑:

    结构节点{
    节点*下一个;
    };

    节点 *node1 = 新节点,node2 = 新节点;

    节点1->下一个=节点2;
    节点2->下一个=节点1;

    由于现在每个节点都引用另一个节点,因此即使没有其他节点引用其中任何一个,它们的引用计数也将保持非零。即使它们变得无法访问,它们也不会被收集。不过,这个问题是可以克服的。

  3. 当您在 C++ 中使用第三方垃圾收集器时,结果不再(完全)是符合 C++ 的实现。例如,如果您“混合”一个指针(例如反转其所有位),GC 将无法识别它指向的内容。当您“取消调配”它(将位向后翻转)时,它所指向的内容可能不再存在。然而,实际代码中的问题非常罕见,人们经常使用 GC 不会出现任何问题。

    OTOH,也有限制,所以大多数 C 和 C++ 垃圾收集器也不能很好地工作。最近的 GC 设计通过复制仍然存在的对象来工作,以便在 GC 循环发生后它们在堆中都是连续的。为此,它必须“修复”所有指向该对象的指针以指向新地址。由于使用 C 或 C++ 的 GC 不确定什么是指针,所以它无法修改事物(如果某些东西不是指针),因此它必须将对象保留在原处,这会损害性能。

关于将 GC 添加到 C++ 中,已经有一些认真的讨论。 两个 comp.lang.c++.moderated 线程可能很有趣。警告:它们相当长,并且某些论点会重复,在某些情况下会重复几次。 OTOH,他们确实指出了一些实际问题和可能的解决方案。

  1. As Alex pointed out, LISP has had garbage collection since well before C++ was invented. OTOH, some of those early implementations used reference counting.

  2. Most people discussing garbage collection are thinking of something that's hidden. Reference counting does have a number of problems. It can have performance problems, but as objects are normally used in C++, rarely does. A much bigger problem is that reference counting doesn't normally deal well with cycles in the data. For a trivial example, consider:

    struct node {
    node *next;
    };

    node *node1 = new node, node2 = new node;

    node1->next = node2;
    node2->next = node1;

    Since each node now references the other, their reference counts will remain non-zero, even if nothing else refers to either one. They aren't collected, even after they become inaccessible. This problem is surmountable, however.

  3. When you use the third party garbage collectors with C++, the result isn't (quite) a conforming implementation of C++ any more. For example, if you "swizzle" a pointer (e.g. invert all its bits) the GC won't recognize what it points at. When you "un-swizzle" it (flap the bits back) what it pointed at may no longer exist. Problems in real code, however, are sufficiently unusual that people use GC regularly without any problems.

    OTOH, there are also limitations, so most garbage collectors for C and C++ don't really work very well either. Most recent GC designs work by copying objects that still exist so they're all contiguous in the heap after a GC cycle has taken place. To do this, it has to "fix" all the pointers to that object to point to the new address. Since a GC with C or C++ doesn't know for sure what's a pointer or not, it can't modify things (in case something isn't a pointer) so it has to leave objects in place, which hurts performance.

There have been some serious discussions about adding GC to C++. Two comp.lang.c++.moderated threads might be interesting. Warning: they are pretty long, and some arguments get repeated, several times in a few cases. OTOH, they do point out some real issues, and possible solutions.

鸩远一方 2024-08-30 06:12:58

关于 1 和较小程度的 2:

这是一个简洁但很好的解释。

http://www.devx.com/tips/Tip/12766

另外还有此处发布了有关该主题的类似问题。

为什么 C++ 没有垃圾收集器?

查看它们。

With regards to 1 and to a lesser degree 2:

This is a concise yet good explanation.

http://www.devx.com/tips/Tip/12766

Plus there is this similar question posted on here about the topic.

Why doesn't C++ have a garbage collector?

Check them out.

平生欢 2024-08-30 06:12:58

根据 http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29< /a> 垃圾收集发明于 1959 年,大大早于 C++。

C++ 不包含垃圾收集器的原因是因为您(程序员)管理内存,这是从 C 继承的功能。垃圾收集器是为您完成内存管理的语言的一部分。您可以实现一个,但您必须使用您的实现来管理所有内存访问 - 否则,您将绕过它。简而言之,GC 是您(无论您使用什么语言)和系统底层内存之间的一层的一部分。

According to http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 Garbage collection was invented in 1959 which would significantly pre-date C++.

The reason C++ does not include a garbage collector is because you, the programmer, manage the memory, a feature inherited from C. A garbage collector is part of a language where memory management is done for you. You could implement one but you'd have to use your implementation to manage all of your memory access - otherwise, you'd be circumventing it. In short, GC is part of a layer between you (in whatever language you're in) and the underlying memory of the system.

雨落□心尘 2024-08-30 06:12:58

对垃圾收集的最低限度支持 - 但没有垃圾收集器 - 将添加到下一个 C++ 标准(非正式称为 C++0x)。这是一篇关于它的好论文: http://www.hpl .hp.com/techreports/2009/HPL-2009-360.pdf

Minimal support for garbage collection - but no garbage collector - will be added to the next C++ standard (informally called C++0x). Here's a good paper about it: http://www.hpl.hp.com/techreports/2009/HPL-2009-360.pdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文