简体   繁体   English

使用AutoZone垃圾收集器

[英]Using AutoZone garbage collector

Has anyone tried to use the autozone garbage collector from Apple? 有没有人尝试使用Apple提供的autozone垃圾收集器 Or can you point to a good and configurable one usable with C++? 还是可以指出一个可与C ++一起使用的良好且可配置的工具?

edit: I work on decision diagrams (like BDD), so I would like to test if managing the memory with a garbage collector is efficient in this case. 编辑:我在决策图(如BDD)上工作,所以我想测试在这种情况下用垃圾回收器管理内存是否有效。

edit 2: To be more precise, when implementing a library for decision diagrams, you HAVE TO implement a garbage collector. 编辑2:更精确地说,在实现决策图库时,必须实现垃圾收集器。 In fact, I already did this for my library, but it represents more or less 25% of the code. 实际上,我已经为我的库做了此操作,但是它代表了大约25%的代码。 And it is the most complicated part :-) So yes, I want a garbage collector :-) And yes, I already use RAII techniques. 这是最复杂的部分:-)所以,是的,我想要一个垃圾收集器:-)是的,我已经在使用RAII技术。 And, finally, I can not afford the cost of a shared_ptr, because I store billions objects which need to be garbage collected. 最后,我负担不起shared_ptr的成本,因为我存储了数十亿个需要进行垃圾回收的对象。

Have you already analyzed if you really need an implicit garbage collection library? 您是否已经分析过是否真的需要隐式垃圾回收库? are you sure it is not just java (or Objective C, ...) nostalgia? 您确定不只是Java(或Objective C,...)怀旧吗?

That is not natural in C++, so you will probably get into more problems than you solve. 这在C ++中并不自然,因此您可能遇到的问题比您要解决的更多。 Actual implementations are mostly used in experimentation tests, and not for production apps. 实际的实现方式主要用于实验测试,而不用于生产应用。 The best way to squeeze the potential of a language is to do things the way are tackled in that language. 发挥语言潜能的最佳方法是按照该语言解决问题的方式做事。

Check first if explicit garbage collection (boost::shared_ptr and friends ) cover your needs, and avoid introducing complexity when possible. 首先检查显式垃圾回收(boost :: shared_ptr和friends )是否满足您的需求,并尽可能避免引入复杂性。

After Alexandre edit 2: Magic does not exist I'm afraid. 亚历山大大帝(Alexandre)编辑2:恐怕不存在魔术。 Why do you think a garbage collector will be more efficient than RAII idioms. 您为什么认为垃圾收集器会比RAII惯用语更有效。

If you don't need reference counting you can use scoped_ptr. 如果不需要引用计数,则可以使用scoped_ptr。 But if you need it, you will have to pay for it, apart from how much you hide it. 但是,如果您需要它,除了要隐藏多少钱之外,您还必须为此付费。

Maybe your problem is allocating dinamically so many objects. 也许您的问题是分配大量的对象。 If they are small ones, you will find really interesting the chapter 4 (Small-Object Allocation) of "Modern C++ Design" (Andrei Alexandrescu). 如果它们很小,您会发现“现代C ++设计”(Andrei Alexandrescu)的第4章(小对象分配)真的很有趣。

Most people tend to avoid garbage collectors in C++. 大多数人倾向于避免使用C ++进行垃圾收集。

They are generally not necessary, once you learn to use RAII to manage your resources, and because C++ does not have proper support for garbage collection, the GC's that exist have a couple of problems: 一旦您学会了使用RAII来管理资源,并且由于C ++对垃圾回收没有适当的支持,通常它们就没有必要了,存在的GC存在两个问题:

  • They don't catch every allocation (they have to make a conservative guess of whether some allocation is referenced or not) 它们不会捕获所有分配(它们必须保守地猜测是否引用了某些分配)
  • They may not play nice with destructors 他们可能不会和破坏者一起玩

Of course there are situations where a GC in C++ is useful. 当然,在某些情况下,C ++中的GC很有用。 But 95% of the cases, you'll be better served simply by learning the appropriate memory management techniques (RAII) yourself. 但是在95%的情况下,只需自己学习适当的内存管理技术(RAII),您就会得到更好的服务。

But I haven't used Autozone, and don't know how well it works in your case or in general. 但是我没有使用过Autozone,也不知道它在您的情况下还是在一般情况下效果如何。

Actually, Garbage Collection was a part of the upcoming C++ 20XX standard, but was dropped for reasons of difficulty of implementation, complexity, etc... 实际上,垃圾收集是即将到来的C ++ 20XX标准的一部分,但由于实现困难,复杂等原因而被删除。

So, sure, lots of people avoid GC in C++, but there is a strong enough demand that the standards committee is actively considering it. 因此,可以肯定的是,很多人都避免使用C ++中的GC,但是有足够的需求,标准委员会正在积极考虑使用它。

Apple's AutoZone is a language agnostic garbage collector that could be bent for use with C++. 苹果公司的AutoZone是与语言无关的垃圾收集器,可以将其与C ++一起使用。 Certainly, that AutoZone works for Objective-C (and C) would make for a good foundation implementation. 当然,AutoZone可用于Objective-C(和C)将为良好的基础实现提供条件。

AutoZone is also used by the MacRuby project and, I believe, a handful of other projects. MacRuby项目以及我相信的其他一些项目也使用AutoZone。 It is designed to be portable, though the implementation has bits specific to the x86 and ppc architectures -- you would need to port it to other CPU types, if necessary. 尽管该实现具有特定于x86和ppc架构的位,但它还是设计为可移植的-如有必要,您需要将其移植到其他CPU类型。

The collector has an API that can be used directly to register/unregister objects and express connectivity, etc... 收集器具有一个API,可直接用于注册/注销对象并表达连接性等。

It wouldn't be easy, but it is certainly doable. 这并非易事,但肯定是可行的。

No I haven't tried that. 不,我没有尝试过。 You may try this from hp labs, with more details going here . 您可以从hp实验室尝试操作,更多详细信息请点击这里 This collector works on Linux, *BSD, recent Windows versions, MacOS X, HP/UX, Solaris, Tru64, Irix and a few other operating systems. 该收集器可在Linux,* BSD,最新的Windows版本,MacOS X,HP / UX,Solaris,Tru64,Irix和其他一些操作系统上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM