我问这个问题,首先不是因为垃圾收集的优点。我问这个问题的主要原因是我知道Bjarne Stroustrup说过c++在某个时间点上会有一个垃圾收集器。
话虽如此,为什么还没有加入呢?c++已经有了一些垃圾收集器。这是那种“说起来容易做起来难”的事情吗?或者还有其他原因没有添加它(并且不会在c++ 11中添加)?
交叉链接:
c++的垃圾收集器
澄清一下,我理解c++最初创建时没有垃圾收集器的原因。我想知道为什么不能添加收集器。
我问这个问题,首先不是因为垃圾收集的优点。我问这个问题的主要原因是我知道Bjarne Stroustrup说过c++在某个时间点上会有一个垃圾收集器。
话虽如此,为什么还没有加入呢?c++已经有了一些垃圾收集器。这是那种“说起来容易做起来难”的事情吗?或者还有其他原因没有添加它(并且不会在c++ 11中添加)?
交叉链接:
c++的垃圾收集器
澄清一下,我理解c++最初创建时没有垃圾收集器的原因。我想知道为什么不能添加收集器。
当前回答
主要有两个原因:
因为它不需要(恕我直言) 因为它与RAII几乎不兼容,RAII是c++的基石
c++已经提供了手动内存管理、堆栈分配、RAII、容器、自动指针、智能指针……这应该足够了。垃圾收集器适合懒惰的程序员,他们不想花5分钟思考谁应该拥有哪些对象或什么时候应该释放资源。这不是我们在c++中做事情的方式。
其他回答
c++背后的思想是,你不需要为你不使用的特性付出任何性能上的影响。因此,添加垃圾收集意味着让一些程序像C语言那样直接在硬件上运行,而另一些则在某种运行时虚拟机中运行。
没有什么可以阻止您使用绑定到某些第三方垃圾收集机制的某种形式的智能指针。我似乎记得微软在COM上做过类似的事情,但并不顺利。
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语言允许编译器施加额外的限制,但C语言包括——c++保留了——将指针分解为字节序列,将包含相同值的任何字节序列组装为指针,然后使用该指针访问先前的对象。
While that ability can be useful--or even indispensable--in some kinds of applications, a language that includes that ability will be very limited in its ability to support any kind of useful and reliable garbage collection. If a compiler doesn't know everything that has been done with the bits that made up a pointer, it will have no way of knowing whether information sufficient to reconstruct the pointer might exist somewhere in the universe. Since it would be possible for that information to be stored in ways that the computer wouldn't be able to access even if it knew about them (e.g. the bytes making up the pointer might have been shown on the screen long enough for someone to write them down on a piece of paper), it may be literally impossible for a computer to know whether a pointer could possibly be used in the future.
An interesting quirk of many garbage-collected frameworks is that an object reference not defined by the bit patterns contained therein, but by the relationship between the bits held in the object reference and other information held elsewhere. In C and C++, if the bit pattern stored in a pointer identifies an object, that bit pattern will identify that object until the object is explicitly destroyed. In a typical GC system, an object may be represented by a bit pattern 0x1234ABCD at one moment in time, but the next GC cycle might replace all references to 0x1234ABCD with references to 0x4321BABE, whereupon the object would be represented by the latter pattern. Even if one were to display the bit pattern associated with an object reference and then later read it back from the keyboard, there would be no expectation that the same bit pattern would be usable to identify the same object (or any object).
虽然这是一个老问题,但仍然有一个问题我没有看到任何人解决过:垃圾收集几乎不可能指定。
特别是,c++标准非常谨慎地根据外部可观察到的行为来指定语言,而不是实现如何实现该行为。然而,在垃圾收集的情况下,实际上没有外部可观察到的行为。
The general idea of garbage collection is that it should make a reasonable attempt at assuring that a memory allocation will succeed. Unfortunately, it's essentially impossible to guarantee that any memory allocation will succeed, even if you do have a garbage collector in operation. This is true to some extent in any case, but particularly so in the case of C++, because it's (probably) not possible to use a copying collector (or anything similar) that moves objects in memory during a collection cycle.
如果不能移动对象,就不能创建一个单独的、连续的内存空间来进行分配——这意味着您的堆(或自由存储区,或任何您喜欢称呼它的地方)可能会随着时间的推移而变得碎片化。这反过来又会阻止分配成功,即使空闲内存比请求的内存多。
尽管有可能提出某种保证,即(本质上)如果您重复完全相同的分配模式,并且它在第一次成功,那么它将在后续迭代中继续成功,前提是分配的内存在迭代之间变得不可访问。这是一个非常微弱的保证,基本上毫无用处,但我看不到任何加强它的合理希望。
即便如此,它也比为c++所提议的更强大。之前的提议[警告:PDF](被放弃了)根本不能保证任何东西。在28页的提案中,你在外部可观察到的行为中看到的是一个单一的(不规范的)注释:
[注意:对于垃圾收集程序,高质量的托管实现应该尝试最大限度地回收不可访问的内存量。-结束注释]
至少对我来说,这引发了一个关于投资回报的严肃问题。我们将破坏现有的代码(没有人知道具体破坏了多少,但肯定是相当多),对实现提出新的要求,对代码提出新的限制,而我们得到的回报很可能是什么都没有?
即使在最好的情况下,我们得到的是基于Java测试的程序,以现在相同的速度运行可能需要大约6倍的内存。更糟糕的是,垃圾收集从一开始就是Java的一部分——c++对垃圾收集器施加了足够多的限制,以至于它几乎肯定会有更糟糕的成本/收益比(即使我们超出了提案所保证的范围,并假设会有一些收益)。
我要用数学方法总结一下情况:这是一个复杂的情况。数学家都知道,复数有两部分:实数和虚数。在我看来,我们这里的成本是真实的,但收益(至少大部分)是虚构的。
为了增加争论。
关于垃圾收集有一些已知的问题,了解它们有助于理解为什么c++中没有垃圾收集。
1. 性能?
第一个抱怨通常是关于性能,但大多数人并没有真正意识到他们在谈论什么。正如马丁·贝克特(Martin Beckett)所指出的,问题可能不是表现本身,而是表现的可预测性。
目前有两个GC家族被广泛部署:
标记和清扫类 引用计数类型
标记和清除更快(对整体性能的影响较小),但它患有“冻结世界”综合征:即当GC开始时,其他一切都停止,直到GC完成清理。如果您希望构建一个在几毫秒内响应的服务器……有些交易不会达到你的期望:)
The problem of Reference Counting is different: reference-counting adds overhead, especially in Multi-Threading environments because you need to have an atomic count. Furthermore there is the problem of reference cycles so you need a clever algorithm to detect those cycles and eliminate them (generally implement by a "freeze the world" too, though less frequent). In general, as of today, this kind (even though normally more responsive or rather, freezing less often) is slower than the Mark And Sweep.
I have seen a paper by Eiffel implementers that were trying to implement a Reference Counting Garbage Collector that would have a similar global performance to Mark And Sweep without the "Freeze The World" aspect. It required a separate thread for the GC (typical). The algorithm was a bit frightening (at the end) but the paper made a good job of introducing the concepts one at a time and showing the evolution of the algorithm from the "simple" version to the full-fledged one. Recommended reading if only I could put my hands back on the PDF file...
2. 资源获取初始化(RAII)
在c++中,将资源的所有权包装在对象中以确保它们被正确地释放是一种常见的习惯用法。它主要用于内存,因为我们没有垃圾回收,但它对许多其他情况也很有用:
锁(多线程,文件句柄,…) 连接(到数据库、另一台服务器……)
其思想是正确地控制对象的生命周期:
只要你需要,它就应该是活的 当你用完它的时候,它应该被杀死
GC的问题在于,如果它有助于前者,并最终保证以后……这个“终极”可能还不够。如果你释放一个锁,你真的希望它现在被释放,这样它就不会阻止任何进一步的调用!
带有GC的语言有两种解决方法:
当堆栈分配足够时不要使用GC:这通常是为了解决性能问题,但在我们的例子中,它确实有帮助,因为作用域定义了生命周期 使用构造…但它是显式(弱)RAII,而在c++中RAII是隐式的,因此用户不能在不知不觉中犯错误(通过省略using关键字)
3.智能指针
在c++中,智能指针通常是处理内存的灵丹妙药。我经常听到:我们根本不需要GC,因为我们有智能指针。
这是大错特错了。
智能指针确实有帮助:auto_ptr和unique_ptr使用RAII概念,确实非常有用。它们很简单,你可以很容易地自己写出来。
然而,当一个人需要共享所有权时,它变得更加困难:你可能在多个线程之间共享,并且在计数的处理上有一些微妙的问题。因此,很自然地使用shared_ptr。
这很棒,毕竟这就是Boost的用途,但它并不是万能的。事实上,shared_ptr的主要问题是它模拟了一个由引用计数实现的GC,但你需要自己实现周期检测…开始
当然有这个weak_ptr的东西,但不幸的是,我已经看到内存泄漏尽管使用shared_ptr,因为这些周期…当你在多线程环境中,它是非常难以检测的!
4. 解决方案是什么?
没有什么灵丹妙药,但一如既往,这绝对是可行的。在没有GC的情况下,需要明确所有权:
如果可能的话,最好在一个特定的时间拥有一个所有者 如果没有,请确保您的类图没有与所有权相关的任何循环,并通过weak_ptr的微妙应用来打破它们
所以,如果有一个GC…然而,这不是一个微不足道的问题。与此同时,我们只需要卷起袖子。