Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection Libraries in C++ [closed]

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.

like image 339
Andrew Bettison Avatar asked Sep 17 '08 08:09

Andrew Bettison


People also ask

Why there is no garbage collector in C?

There are two reasons why C / C++ doesn't have garbage collection. It is "culturally inappropriate". The culture of these languages is to leave storage management to the programmer. It would be technically difficult (and expensive) to implement a precise garbage collector for C / C++.

Does C support garbage collection?

C does not have automatic garbage collection. If you lose track of an object, you have what is known as a 'memory leak'. The memory will still be allocated to the program as a whole, but nothing will be able to use it if you've lost the last pointer to it. Memory resource management is a key requirement on C programs.

Does C++ 11 have garbage collection?

Garbage collection (automatic recycling of unreferenced regions of memory) is optional in C++; that is, a garbage collector is not a compulsory part of an implementation. However, C++11 provides a definition of what a GC can do if one is used and an ABI (Application Binary Interface) to help control its actions.

Which programming language does not have garbage collection?

This is one of many reasons why languages like Java and C# are slower than C and C++ by design. And it is also the reason why C and C++ don't have and never will have a garbage collector, since those languages prioritize execution speed.


2 Answers

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.

like image 172
Greg Hewgill Avatar answered Oct 20 '22 09:10

Greg Hewgill


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.

like image 20
Tom Leys Avatar answered Oct 20 '22 10:10

Tom Leys