Garbage collection Libraries in C++ [closed]

2019-01-10 07:12发布

What free and commercial garbage collection libraries are available for C++, and what are the pros and cons of each?

I am interested in hard-won lessons from actual use in the field, not marketing or promotional blurb.

There is no need to elaborate on the usual trade offs associated with automatic garbage collection, but please do mention the algorithms used (reference counting, mark and sweep, incremental, etc.) and briefly summarise the consequences.

9条回答
祖国的老花朵
2楼-- · 2019-01-10 07:46

The major difficulty with GC's in C++ is the need to handle uncooperative modules, in the GC sense. ie, to deal with libraries that were never written with GC's in mind.

This is why the Boehm GC is often suggested.

查看更多
Anthone
3楼-- · 2019-01-10 07:48

I have used the Boehm collector in the past with good success. It's open source and can be used in commercial software.

It's a conservative collector, and has a long history of development by one of the foremost researchers in garbage collection technology.

查看更多
神经病院院长
4楼-- · 2019-01-10 07:51

You can also use Microsoft's Managed C++. The CLR and the GC are very solid and used in server products, but you have to use CLR types for the GC to actually collect - you can't just recompile your existing code and remove all the delete statements.

I would rather use C# to write brand new code, but Managed C++ lets you evolve your code base in a more progressive manner.

查看更多
放我归山
5楼-- · 2019-01-10 07:56

I use boehm-gc a lot. It is straight-forward to use, but the documentation is really poor. There is a C++ page, but its quite hard to find.

Basically, you just make sure that every class inherits from their base class, and that you always pass gc_allocator to a container. In a number of cases you want to use libgccpp to catch other uses of new and delete. These are largely high-level changes, and we find that we can turn off the GC at compile-time using an #ifdef, and that supporting this only affects one or two files.

My major problem with it is that you can no longer use Valgrind, unless you turn the collector off first. While turning the collector off is easy to do, and doesn't require recompiling, it's obviously impossible to use it if you start to run out of memory.

查看更多
干净又极端
6楼-- · 2019-01-10 07:59

Boost has a great range of smart pointers which impliment reference counting or delete-on-scope exit or intrusive reference counting. These have proven enough for our needs. A big plus is that it is all free, open source, templated C++. because it is reference counting, in most cases it is highly deterministic when an object gets destroyed.

查看更多
7楼-- · 2019-01-10 08:01

The Boehm garbage collector is freely available, and supposedly rather good (no first hand experience myself)

([PDF WARNING]Theoretical paper about C++0x proposal for the Boehm garbage collector)

It was originally said to make C++0x , but will not make it after all (due to time constraints I suppose).

Proprosal N2670 (minimal support for garbage collectors) did get approved in june 2008 though, so as compiler implementations pick up on this, and the standard gets finalised, the garbage collection world out there for C++ is sure to change...

查看更多
登录 后发表回答