Click here to Skip to main content
13,297,048 members (55,897 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 30 Jul 2004

AGM::LibGC: a C++ garbage collection library

, 30 Jul 2004
Rate this:
Please Sign up or sign in to vote.
AGM::LibGC is a C++ garbage collection library.


LibGC is a very small library (around 500 lines of code) that gives garbage collection capabilities to C++. It is developed, tested, and used under MSVC++ 6.0.

The usual solution for memory management in the C++ environment (aside from using garbage collectors, like the Hans - Boehm one) is reference counting. But reference counting has some fundamental problems:

  1. cyclic references
  2. slow speed of execution
  3. difficulty in programming

The problem no. 1) is due to having a counter that counts how many other objects point to a certain object; the counter of the object must reach 0 in order for the object to be released. Unfortunately, when one object A points to another object B and that object B points back to A, these two objects will never be released since there is a cycle between them.

The problem no. 2) is due to the way reference counting works. Within every assignment, the managed pointer class has to do the following steps:

  1. lock resources (if the solution is multithreaded)
  2. compare current pointer with new one; if there is no change, do nothing
  3. store the current pointer value in a temporary variable
  4. copy the value of the parameter pointer to the pointer member
  5. increase the reference count of the new object
  6. decrease the reference count of the previous object
  7. delete the previous object, if the reference count reaches 0
  8. unlock resources (if the solution is multithreaded)

All the above operations take place in every assignment; they are very expensive and can make a program very slow, especially if the program has thousands of pointers (for example, on the stack).

The problem no. 3) is due to the way reference counting works. Special care should be taken in handcrafting destructors in order to avoid multiple deletions of the same memory block, a task that is quite complicated. Many programmers tend to make wrapper classes around shared objects in order to avoid this problem, but then they have to manage two classes with the same interface: the wrapper class and the internal class.

The solution offered by LibGC has none of these disadvantages. Objects that are referenced in cycles are collected and deleted normally, the library is generally quite fast in doing garbage collection, and there is no need for wrapper classes.

LibGC uses the conservative mark-and-sweep stop-the-world algorithm to collect the garbage data. The programmer writes C++ as usual, with pointers and such, but objects need not be deleted. Object deletions work as usual.

The license of LibGC is the LGPL.

Using LibGC is very simple; just program normally as you always did:

  • allocate objects with operator new.
  • allocate objects that need finalization with macro GC_NEW.
  • inherit your classes from class Object in order to be automatically finalized without the usage of GC_NEW.
  • if you need to delete objects, delete them with operator delete.
  • declare objects on the stack.
  • declare objects globally (in the program's data section).
  • manually do garbage collection by calling the function doGC().

Deletions work normally: you can delete an object anytime, and the memory occupied by the object is no longer garbage-collected.

When the application exits, all objects are finalized, and memory is freed.

The class Object is in the namespace agm::gc.


//use garbage collection
#include "gc.hpp"
using namespace agm::gc;

//include some STL class for demonstrating the global use of operator 'new'
#include <list>
using namespace std;

//a class that is not automatically finalized
class Foo {

//a class that is automatically finalized
class Bar : public Object {

int main()
    //allocate a Foo object with macro GC_NEW 
    //because it is not automatically finalized
    Foo *obj1 = GC_NEW(Foo)();

    //allocate an STL list that is not automatically finalized;
    //nodes will be freed by the collector
    list<INT> *obj2 = new list<INT>;

    //allocate a Bar object with operator 'new' 
    //that is automatically finalized
    //since it inherits from Object
    Bar *obj3 = new Bar;

The library will collect garbage automatically as soon as more than GC_THRESHOLD bytes have not been collected.

  • Note 1: The library has been developed and tested under MSVC++ 6.0. It will probably run under other compilers, too. If you want to test it and tell me, feel free to do so: you will be added in the list of contributors.
  • Note 2: Exceptions must be enabled.
  • Note 3: The solution does not work for multithreaded applications (yet).

For more information, you may visit my little site here.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Achilleas Margaritis
Software Developer (Senior)
Greece Greece
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralLibGC error Pin
caoctaviano25-Jun-09 10:46
membercaoctaviano25-Jun-09 10:46 
GeneralProblem with libgc Pin
pulkownik1127-Mar-08 0:37
memberpulkownik1127-Mar-08 0:37 
QuestionA question in reading your code Pin
nightelf19843-Jun-07 22:03
membernightelf19843-Jun-07 22:03 
QuestionIs it thread safe? Pin
Aliff9-Oct-04 20:07
memberAliff9-Oct-04 20:07 
GeneralAn interesting possible problem Pin
Nathan Holt at CCEI3-Aug-04 8:07
memberNathan Holt at CCEI3-Aug-04 8:07 
GeneralRe: An interesting possible problem Pin
axilmar4-Aug-04 0:49
memberaxilmar4-Aug-04 0:49 
GeneralRe: An interesting possible problem Pin
Edwin G. Castro10-Aug-04 9:17
memberEdwin G. Castro10-Aug-04 9:17 
GeneralMingW32 support. Pin
axilmar3-Aug-04 7:19
memberaxilmar3-Aug-04 7:19 
GeneralGarbage Pin
TW2-Aug-04 17:33
memberTW2-Aug-04 17:33 
GeneralRe: Garbage Pin
axilmar3-Aug-04 1:45
memberaxilmar3-Aug-04 1:45 
GeneralRe: Garbage Pin
Nemanja Trifunovic3-Aug-04 6:58
memberNemanja Trifunovic3-Aug-04 6:58 
GeneralRe: Garbage Pin
axilmar3-Aug-04 7:21
memberaxilmar3-Aug-04 7:21 
GeneralRe: Garbage Pin
Nathan Holt at CCEI3-Aug-04 7:53
memberNathan Holt at CCEI3-Aug-04 7:53 
GeneralRe: Garbage Pin
Nemanja Trifunovic3-Aug-04 7:56
memberNemanja Trifunovic3-Aug-04 7:56 
GeneralRe: Garbage Pin
axilmar4-Aug-04 0:52
memberaxilmar4-Aug-04 0:52 
GeneralRe: Garbage Pin
Nathan Holt at CCEI4-Aug-04 6:47
memberNathan Holt at CCEI4-Aug-04 6:47 
GeneralRe: Garbage Pin
axilmar4-Aug-04 7:44
memberaxilmar4-Aug-04 7:44 
GeneralRe: Garbage Pin
Nathan Holt at CCEI4-Aug-04 8:17
memberNathan Holt at CCEI4-Aug-04 8:17 
axilmar wrote:
Nope, they can't. Suppose that you have:
class Parent {
    SmartPtr child;
class Child {
    SmartPtr parent;
int main()
    SmartPtr p = new Parent;
    p->child = new Child;

If the pointer to the parent is needed, why should it be a smart pointer? Also, unless the parent is sharing its child, its pointer would not be a reference counting pointer, but instead be a simpler scoped pointer like boost::scoped_ptr<Child>

In the rare cases in which a parent class does share its child classes with the possibility of outliving their parent, I have made them communicate with their parent with a signal/slot type system, which is designed to automatically disconnect when either object is destroyed.

axilmar wrote:
There is also the other problem of double deletions. For example:

<pre>class Foo {<br />
public:<br />
    SmartPtr foo;<br />
};<br />
<br />
int main(){<br />
    Foo foo1;<br /> = &foo1;<br />

In the above code, when the smart pointer will be deleted, it will delete the object it belongs!

I see what you meant by double deletions. I don't think I've ever created a reference counted object on the stack. For that matter, hardly any of my reference counted objects have contained reference counting pointers.

Nathan Holt
GeneralRe: Garbage Pin
axilmar5-Aug-04 1:05
memberaxilmar5-Aug-04 1:05 
GeneralRe: Garbage Pin
Nathan Holt at CCEI5-Aug-04 7:44
memberNathan Holt at CCEI5-Aug-04 7:44 
GeneralRe: Garbage Pin
axilmar5-Aug-04 11:04
memberaxilmar5-Aug-04 11:04 
GeneralRe: Garbage Pin
Nathan Holt at CCEI6-Aug-04 7:59
memberNathan Holt at CCEI6-Aug-04 7:59 
GeneralRe: Garbage Pin
axilmar8-Aug-04 1:40
memberaxilmar8-Aug-04 1:40 
GeneralRe: Garbage Pin
Nathan Holt at CCEI9-Aug-04 7:48
memberNathan Holt at CCEI9-Aug-04 7:48 
GeneralRe: Garbage Pin
Nemanja Trifunovic4-Aug-04 17:27
memberNemanja Trifunovic4-Aug-04 17:27 
GeneralRe: Garbage Pin
axilmar5-Aug-04 1:06
memberaxilmar5-Aug-04 1:06 
GeneralVersion 2.1 is out. Pin
axilmar2-Aug-04 7:12
memberaxilmar2-Aug-04 7:12 
GeneralDoes not work with MFC apps in Debug build Pin
FD2-Aug-04 0:25
memberFD2-Aug-04 0:25 
GeneralRe: Does not work with MFC apps in Debug build Pin
axilmar2-Aug-04 0:57
memberaxilmar2-Aug-04 0:57 
GeneralQuick questions... Pin
Ed Preston1-Aug-04 22:28
memberEd Preston1-Aug-04 22:28 
GeneralRe: Quick questions... Pin
Ed Preston1-Aug-04 22:50
memberEd Preston1-Aug-04 22:50 
GeneralRe: Quick questions... Pin
axilmar2-Aug-04 1:22
memberaxilmar2-Aug-04 1:22 
GeneralMultithreaded GC Pin
Nathan Holt at CCEI3-Aug-04 6:32
memberNathan Holt at CCEI3-Aug-04 6:32 
GeneralRe: Multithreaded GC Pin
axilmar3-Aug-04 7:30
memberaxilmar3-Aug-04 7:30 
GeneralRe: Quick questions... Pin
axilmar2-Aug-04 1:04
memberaxilmar2-Aug-04 1:04 
GeneralRe: Quick questions... Pin
Ed Preston2-Aug-04 3:35
memberEd Preston2-Aug-04 3:35 
GeneralRe: Quick questions... Pin
axilmar2-Aug-04 7:08
memberaxilmar2-Aug-04 7:08 
GeneralRe: Quick questions... Pin
Jim Crafton3-Aug-04 4:07
memberJim Crafton3-Aug-04 4:07 
GeneralRe: Quick questions... Pin
Anonymous3-Aug-04 6:21
sussAnonymous3-Aug-04 6:21 
GeneralRe: Quick questions... Pin
axilmar3-Aug-04 7:22
memberaxilmar3-Aug-04 7:22 
GeneralRe: Quick questions... Pin
axilmar3-Aug-04 7:22
memberaxilmar3-Aug-04 7:22 
Questioncould you please elaborate? Pin
peterchen1-Aug-04 1:00
memberpeterchen1-Aug-04 1:00 
AnswerRe: could you please elaborate? Pin
axilmar1-Aug-04 1:34
memberaxilmar1-Aug-04 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 31 Jul 2004
Article Copyright 2004 by Achilleas Margaritis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid