72

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.

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
Andrew Bettison
  • 721
  • 1
  • 5
  • 3
  • 11
    **Reason for a reopen**: At the time of the original posting were the software recommendations not yet forbidden on the SO. Applying rules retroactively is an evil deed, especially because there is also the possibility of marking the question as historical, and this is what I suggest to do. So I marked the question for a reopen. – peterh Apr 06 '16 at 07:24
  • See http://managedcpp.sourceforge.net/ using BoehmGC but for C++ specifically using C++11. – Zach Saw Jun 21 '16 at 02:27

9 Answers9

31

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.

dubek
  • 11,447
  • 5
  • 30
  • 23
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • A little remark: Boehm GC doesn't follow the pointers and the allocated memory areas. It doesn't even know if a memory address is a pointer or not. Thus 1) with Boehm GC remains the possibility of unused memory areas which can't be deallocated any more (if they are "referred" by a data which is not a pointer). 2) boehm GC also scans the non-pointer data for "pointers", if you have a few pointers but many data (f.e. you have many large data blocks), it means a significant overhead 3) Boehm GC is pure C and uses hardly the C preprocessor 4) it is not thread safe. – peterh Apr 06 '16 at 07:29
23

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.

Tom Leys
  • 18,473
  • 7
  • 40
  • 62
  • I use the auto_ptr with a great deal of success. – Kieveli Jun 12 '09 at 14:52
  • 8
    Gack. Auto_ptr should be dragged out behind the barn and shot. It has it's uses, but children: never mix auto_ptr and collections. Bad juju results. – Chris K Jul 29 '09 at 14:13
  • 1
    @darthcoder - Agreed, but any C++ implementation that *allows* you to mix auto_ptr and collections should also be dragged out behind the barn, etc. – Daniel Earwicker Jul 29 '09 at 14:17
  • 9
    @Tom Leys - it should be born in mind that reference counted smart pointers are NOT an implementation of GC. They don't automatically take care of cyclic references and in real applications they have worse performance (lots of unnecessary book-keeping, all carried out during times of heaviest load instead of at idle-time). – Daniel Earwicker Jul 29 '09 at 14:19
  • 5
    I'd also question the idea that `shared_ptr` is usefully "deterministic". A destructor on a local object instance runs deterministically - we know that it executes when the enclosing block is exited. But in the same situation with a `shared_ptr`, all we know is that the refcount has been decremented. The whole point of `shared_ptr` is that we don't know whether the object is still needed elsewhere, so we cannot determine locally when it has been destroyed. If you need a "file handle" object to be closed by the time you exit some scope, don't use `shared_ptr`. – Daniel Earwicker Jul 29 '09 at 14:28
  • All good points Earwicker. I would not suggest that you use reference counting on small throwaway objects, only on the larger classes that are few in number and last a relatively long time. – Tom Leys Jul 29 '09 at 22:51
  • @Daniel: `unique_ptr`s should be the primarily used smart pointer. But `unique_ptr`s aren't well supported in c++03 so we see a lot more use of `shared_ptr`s than we ought to. Every pointer is a shared pointer does not improve c++ code quality. Every pointer is a smart pointer dramatically improves code quality. But the lifetime of every complex object must be reasoned about. Most GC'd code (C#,Java,etc) could be improved by careful developer reasoning about instance lifetimes. GC'd languages let you ignore memory lifetime issues but other resource classes still need consideration. – deft_code Jan 20 '11 at 15:55
  • The smart pointers of the boost do only a reference counting in the best case, or they can help to use RAII. Graph-walking GC is simply unimplemented in boost, and it will probably always so, especially on a lock-based, multithreaded way. – peterh Apr 06 '16 at 07:31
11

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

Theoretical paper (in PDF) 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...

Amin Ya
  • 1,515
  • 1
  • 19
  • 30
Pieter
  • 17,435
  • 8
  • 50
  • 89
  • Afaik the proposal N2670 is a nice try to insert Boehm GC which has its serious fallbacks detailed [here](https://stackoverflow.com/questions/81062/garbage-collection-libraries-in-c#comment60502164_81116). – peterh Apr 06 '16 at 07:33
10

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.

Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
  • Afaik Boehm GC may be useful in some projects, but it has serious fallbacks especially for near-java tasks. For example, it is not thread-safe, or see data as pointers. Detailed list is [here](https://stackoverflow.com/questions/81062/garbage-collection-libraries-in-c#comment60502164_81116). – peterh Apr 06 '16 at 07:34
  • The C++ portion of BoehmGC is very incomplete. See http://managedcpp.sourceforge.net/ for an example of how it could be used to have actual precise GC with C++11. – Zach Saw Jun 21 '16 at 02:30
2

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.

Arafangion
  • 11,517
  • 1
  • 40
  • 72
  • Fortunately, in the case of compiled java code, the possibility of using non-gc libraries is much smaller. – peterh Apr 06 '16 at 07:38
2

The only one I know of is Boehm, which at the bottom is a traditional mark and sweep. It probably uses various techniques to optimize this, but typically incremental/generational/compacting GC's will be hard to create for C++ without going for a managed subset such as what you can get with .Net C++. Some of the approaches that needs to move pointers can be implemented with compiler support for pinning pointers or read/write blocks though, but the effect on performance may be too big, and it isn't necessarily non-trivial changes to the GC.

larsivi
  • 1,485
  • 10
  • 16
  • Well, NO GC need program to be interpreted... Maybe you mean it needs compiler support. – dev1223 Jan 03 '16 at 10:52
  • Afaik Boehm GC may be useful in some projects, but it has serious fallbacks especially for near-java tasks. For example, it is not thread-safe, or see data as pointers. Detailed list is [here](https://stackoverflow.com/questions/81062/garbage-collection-libraries-in-c#comment60502164_81116). – peterh Apr 06 '16 at 07:35
1

Here's a commercial product I found in just looking for this same thing

http://www.harnixtechnologies.ca/hnxgc/

Back in the day, there was also a product called Great Circle from Geodesic Systems, but doesn't look like they sell that anymore. No idea if the sold the product to anyone else.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Daniel Holmes
  • 342
  • 1
  • 2
  • 13
1

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.

Remi Lemarchand
  • 863
  • 6
  • 4
  • 3
    Disadvantages: 1) it is microsoft-specific 2) it compiles code to the CLR, i.e. for a VM. – peterh Apr 06 '16 at 07:37
1

Read this and take a good look at the conclusions:

Conclusions

  • Complex solution to problem for which simple solutions are widely used and will be improved by C++0x leaving us little need.

  • We have little to no experience with the recommended language features which are to be standardized.

  • Fixing bad software complex system will never work.

  • Recommend minor language changes to improve future GC support - disallow hiding of pointers (xor list trick) as one example.

  • Finally - address the "C++ is bad because it has no GC" argument head-on. C++ doesn't generate garbage and so has no need for GC. Clearly Java, C#, Objective C, etc. generate lots of garbage.

Yes the last sentence is subjective and also a part of the holy wars.
I use C++ because I dislike the idea that someone needs to take out the garbage for me.
The city hall does that and that's enough for me.
If you need GC use another language. Pick the right tool for the right job.

Community
  • 1
  • 1
the_drow
  • 18,571
  • 25
  • 126
  • 193
  • 13
    Your conclusion is completely back-to-front. In a C++ program someone has to manually design how the garbage is collected. (It might be you, it might be the next poor guy who has to fix your memory leaks). In a language with integrated GC, *no one has to do this*. It's already been figured out. If you want to reduce the amount of pointless work being done by people, base your work on a GC. – Daniel Earwicker Jul 29 '09 at 14:16
  • In c++ where you need a GC you can often find a more suitable and performant solution. Tipical usecase is scene graph. the scene graph needs to be compressed to wipe out holes in memory. A Gc can do that, but in C++ you don't need to take out garbage (additional and costly task). You just need to use quickly empty memory buffers. – CoffeDeveloper Apr 12 '15 at 18:44
  • Not true, C++ _generates_ garbage from the point of that your data structures are at least on the complexity of ad-hoc graphs. For the GC, you need to use ad hoc solutions, and yes, the everyday experience is that big C++ binaries _do_ leak, despite the significant effort of the ad hoc gc of the developers. (Well, also java processes can leak, but in java, the required work time of the gc is nearly always around zero). – peterh Apr 06 '16 at 07:40