垃圾收集不是为 C 设计的,是否有特定的原因? [英] Was there a specific reason why garbage collection was not designed for C?

查看:19
本文介绍了垃圾收集不是为 C 设计的,是否有特定的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说 C 自动收集垃圾不是最理想的——这是真的吗?

I have heard that it was suboptimal for C to automatically collect garbage — is there any truth to this?

是否有特定原因没有为 C 实现垃圾收集?

Was there a specific reason garbage collection was not implemented for C?

推荐答案

别听C老了,所以没有GC"伙计们.GC 存在无法克服的基本问题,使其与 C 不兼容.

Don't listen to the "C is old and that's why it doesn't have GC" folks. There are fundamental problems with GC that cannot be overcome which make it incompatible with C.

最大的问题是准确的垃圾回收需要能够扫描内存并识别遇到的任何指针.一些高级语言限制整数不使用所有可用的位,因此可以使用高位来区分对象引用和整数.然后,此类语言可以将字符串(可能包含任意八位字节序列)存储在一个特殊的字符串区域中,在那里它们不会与指针混淆,一切都很好.然而,C 实现无法做到这一点,因为字节、较大的整数、指针和其他所有内容都可以一起存储在结构、联合或作为 malloc 返回的块的一部分.

The biggest problem is that accurate garbage collection requires the ability to scan memory and identify any pointers encountered. Some higher level languages limit integers not to use all the bits available, so that high bits can be used to distinguish object references from integers. Such languages may then store strings (which could contain arbitrary octet sequences) in a special string zone where they can't be confused with pointers, and all is well. A C implementation, however, cannot do this because bytes, larger integers, pointers, and everything else can be stored together in structures, unions, or as part of chunks returned by malloc.

如果您放弃精度要求并决定您可以接受一些永远不会被释放的对象,因为程序中的某些非指针数据与这些对象的地址具有相同的位模式?现在假设您的程序从外部世界(网络/文件/等)接收数据.我声称我可以让你的程序泄漏任意数量的内存,并最终耗尽内存,只要我能猜到足够多的指针并在我提供给你的程序的字符串中模拟它们.如果您应用 De Bruijn 序列,这会变得容易得多.

What if you throw away the accuracy requirement and decide you're okay with a few objects never getting freed because some non-pointer data in the program has the same bit pattern as these objects' addresses? Now suppose your program receives data from the outside world (network/files/etc.). I claim I can make your program leak an arbitrary amount of memory, and eventually run out of memory, as long as I can guess enough pointers and emulate them in the strings I feed your program. This gets a lot easier if you apply De Bruijn Sequences.

除此之外,垃圾收集也很慢.你可以找到数百名喜欢另辟蹊径的学者,但这不会改变现实.GC 的性能问题可以分为 3 大类:

Aside from that, garbage collection is just plain slow. You can find hundreds of academics who like to claim otherwise, but that won't change the reality. The performance issues of GC can be broken down into 3 main categories:

  • 不可预测性
  • 缓存污染
  • 花在所有记忆中的时间

如今那些声称 GC 速度很快的人只是将其与错误的事物进行比较:写得很糟糕的 C 和 C++ 程序每秒分配和释放数千或数百万个对象.是的,这些也会很慢,但至少可以预见地很慢,你可以在必要时测量和修复.一个编写良好的 C 程序在 malloc/free 中花费的时间是如此之少,以至于开销甚至无法衡量.

The people who will claim GC is fast these days are simply comparing it to the wrong thing: poorly written C and C++ programs which allocate and free thousands or millions of objects per second. Yes, these will also be slow, but at least predictably slow in a way you can measure and fix if necessary. A well-written C program will spend so little time in malloc/free that the overhead is not even measurable.

这篇关于垃圾收集不是为 C 设计的,是否有特定的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆