我问这个问题,首先不是因为垃圾收集的优点。我问这个问题的主要原因是我知道Bjarne Stroustrup说过c++在某个时间点上会有一个垃圾收集器。

话虽如此,为什么还没有加入呢?c++已经有了一些垃圾收集器。这是那种“说起来容易做起来难”的事情吗?或者还有其他原因没有添加它(并且不会在c++ 11中添加)?

交叉链接:

c++的垃圾收集器

澄清一下,我理解c++最初创建时没有垃圾收集器的原因。我想知道为什么不能添加收集器。


当前回答

所有的技术讨论都使这个概念过于复杂。

如果您将GC自动放入c++以获取所有内存,那么可以考虑使用类似web浏览器的东西。web浏览器必须加载完整的web文档并运行web脚本。web脚本变量可以存储在文档树中。在打开大量选项卡的浏览器中的大文档中,这意味着每次GC必须进行完整收集时,它还必须扫描所有文档元素。

On most computers this means that PAGE FAULTS will occur. So the main reason, to answer the question is that PAGE FAULTS will occur. You will know this as when your PC starts making lots of disk access. This is because the GC must touch lots of memory in order to prove invalid pointers. When you have a bona fide application using lots of memory, having to scan all objects every collection is havoc because of the PAGE FAULTS. A page fault is when virtual memory needs to get read back into RAM from disk.

因此,正确的解决方案是将应用程序分为需要GC的部分和不需要GC的部分。在上面的web浏览器示例中,如果文档树是用malloc分配的,但javascript是用GC运行的,那么每次GC启动时,它只扫描一小部分内存,并且文档树的内存中所有page OUT元素不需要被换回。

为了进一步理解这个问题,请查阅虚拟内存以及它是如何在计算机中实现的。这都是关于当没有那么多RAM时,程序可用2GB的事实。在32BIt系统的2GB RAM的现代计算机上,只要只有一个程序在运行,就不存在这样的问题。

作为另一个示例,考虑一个必须跟踪所有对象的完整集合。首先,您必须扫描所有通过根访问的对象。第二步扫描步骤1中可见的所有对象。然后扫描等待的析构函数。然后再次浏览所有页面,关闭所有不可见对象。这意味着许多页面可能会被多次换出和换回。

因此,我的回答是,由于触及所有内存而发生的PAGE FAULTS的数量导致对程序中所有对象进行完整的GC是不可行的,因此程序员必须将GC视为脚本和数据库工作的辅助,但使用手动内存管理进行正常工作。

另一个很重要的原因当然是全局变量。为了让收集器知道全局变量指针在GC中,它需要特定的关键字,因此现有的c++代码将无法工作。

其他回答

主要有两个原因:

因为它不需要(恕我直言) 因为它与RAII几乎不兼容,RAII是c++的基石

c++已经提供了手动内存管理、堆栈分配、RAII、容器、自动指针、智能指针……这应该足够了。垃圾收集器适合懒惰的程序员,他们不想花5分钟思考谁应该拥有哪些对象或什么时候应该释放资源。这不是我们在c++中做事情的方式。

要回答关于c++的大多数“为什么”问题,请阅读c++的设计与进化

If you want automatic garbage collection, there are good commercial and public-domain garbage collectors for C++. For applications where garbage collection is suitable, C++ is an excellent garbage collected language with a performance that compares favorably with other garbage collected languages. See The C++ Programming Language (4rd Edition) for a discussion of automatic garbage collection in C++. See also, Hans-J. Boehm's site for C and C++ garbage collection (archive). Also, C++ supports programming techniques that allow memory management to be safe and implicit without a garbage collector. I consider garbage collection a last choice and an imperfect way of handling for resource management. That does not mean that it is never useful, just that there are better approaches in many situations.

来源:http://www.stroustrup.com/bs_faq.html垃圾收集

至于为什么它没有内置它,如果我没记错的话,它是在GC出现之前发明的,我不相信这种语言有GC,有几个原因(我不相信它有GC)。C)向后兼容。

希望这能有所帮助。

c++背后的思想是,你不需要为你不使用的特性付出任何性能上的影响。因此,添加垃圾收集意味着让一些程序像C语言那样直接在硬件上运行,而另一些则在某种运行时虚拟机中运行。

没有什么可以阻止您使用绑定到某些第三方垃圾收集机制的某种形式的智能指针。我似乎记得微软在COM上做过类似的事情,但并不顺利。

c++没有内置垃圾回收的最大原因之一是,让垃圾回收很好地使用析构函数是非常非常困难的。据我所知,还没有人真正知道如何完全解决这个问题。有很多问题需要处理:

deterministic lifetimes of objects (reference counting gives you this, but GC doesn't. Although it may not be that big of a deal). what happens if a destructor throws when the object is being garbage collected? Most languages ignore this exception, since theres really no catch block to be able to transport it to, but this is probably not an acceptable solution for C++. How to enable/disable it? Naturally it'd probably be a compile time decision but code that is written for GC vs code that is written for NOT GC is going to be very different and probably incompatible. How do you reconcile this?

这些只是面临的问题中的一小部分。