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

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

<p>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.</p>

[{'answer_id': 81115, 'author': 'Pieter', 'author_id': 5822, 'author_profile': 'https://Stackoverflow.com/users/5822', 'pm_score': 4, 'selected': False, 'text': '<p><a href="https://hboehm.info/gc/" rel="nofollow noreferrer">The Boehm garbage collector</a> is freely available, and supposedly rather good (no first hand experience myself)</p>\n<p>Theoretical paper (in PDF) about <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2310.pdf" rel="nofollow noreferrer">C++0x proposal for the Boehm garbage collector</a></p>\n<p>It was originally said to make C++0x <a href="http://herbsutter.spaces.live.com/Blog/cns!2D4327CC297151BB!330.entry" rel="nofollow noreferrer">, but will not make it after all</a> (due to time constraints I suppose).</p>\n<p><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2670.htm" rel="nofollow noreferrer">Proprosal N2670</a> (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...</p>\n'}, {'answer_id': 81116, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 5, 'selected': False, 'text': '<p>I have used the <a href="http://hboehm.info/gc/" rel="noreferrer">Boehm collector</a> in the past with good success. It\'s open source and can be used in commercial software.</p>\n\n<p>It\'s a conservative collector, and has a long history of development by one of the foremost researchers in garbage collection technology.</p>\n'}, {'answer_id': 81126, 'author': 'larsivi', 'author_id': 14047, 'author_profile': 'https://Stackoverflow.com/users/14047', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 81525, 'author': 'Tom Leys', 'author_id': 11440, 'author_profile': 'https://Stackoverflow.com/users/11440', 'pm_score': 5, 'selected': False, 'text': '<p>Boost has a great range of <a href="http://www.boost.org/doc/libs/1_36_0/libs/smart_ptr/smart_ptr.htm" rel="noreferrer">smart pointers</a> 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.</p>\n'}, {'answer_id': 628993, 'author': 'Arafangion', 'author_id': 52273, 'author_profile': 'https://Stackoverflow.com/users/52273', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>This is why the Boehm GC is often suggested.</p>\n"}, {'answer_id': 986915, 'author': 'Daniel Holmes', 'author_id': 91221, 'author_profile': 'https://Stackoverflow.com/users/91221', 'pm_score': 1, 'selected': False, 'text': '<p>Here\'s a commercial product I found in just looking for this same thing</p>\n<p><a href="http://www.harnixtechnologies.ca/hnxgc/" rel="nofollow noreferrer">http://www.harnixtechnologies.ca/hnxgc/</a></p>\n<p>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.</p>\n'}, {'answer_id': 986967, 'author': 'Remi Lemarchand', 'author_id': 106702, 'author_profile': 'https://Stackoverflow.com/users/106702', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n\n<p>I would rather use C# to write brand new code, but Managed C++ lets you evolve your code base in a more progressive manner.</p>\n"}, {'answer_id': 987132, 'author': 'the_drow', 'author_id': 85140, 'author_profile': 'https://Stackoverflow.com/users/85140', 'pm_score': 1, 'selected': False, 'text': '<p>Read <a href="http://stlab.adobe.com/wiki/index.php/Argument_Against_GC" rel="nofollow noreferrer">this</a> and take a good look at the conclusions:</p>\n<blockquote>\n<p>Conclusions</p>\n<ul>\n<li><p>Complex solution to problem for which simple solutions are widely used and will be improved by C++0x leaving us little need.</p>\n</li>\n<li><p>We have little to no experience with the recommended language features which are to be standardized.</p>\n</li>\n<li><p>Fixing bad software complex system will never work.</p>\n</li>\n<li><p>Recommend minor language changes to improve future GC support - disallow hiding of pointers (xor list trick) as one example.</p>\n</li>\n<li><p>Finally - address the &quot;C++ is bad because it has no GC&quot; argument head-on. <strong>C++ doesn\'t generate garbage and so has no need for GC</strong>. Clearly Java, C#, Objective C, etc. generate lots of garbage.</p>\n</li>\n</ul>\n</blockquote>\n<p>Yes the last sentence is subjective and also a part of the holy wars.<br />\nI use C++ because I dislike the idea that someone needs to take out the garbage for me.<br />\nThe city hall does that and that\'s enough for me.<br />\nIf you need GC use another language. Pick the right tool for the right job.</p>\n'}, {'answer_id': 1200538, 'author': 'Paul Biggar', 'author_id': 104021, 'author_profile': 'https://Stackoverflow.com/users/104021', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n"}]
['https://Stackoverflow.com/questions/81062', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15315/']