Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C++

AGM::LibGC: A C++ Garbage Collection Library

Rate me:
Please Sign up or sign in to vote.
4.67/5 (13 votes)
30 Jul 20043 min read 125.5K   871   28   44
A library for C++ garbage collection
This article discusses a library called AGM::LibGC for garbage collection in C#. We will take a look at three fundamental problems of reference counting, and state how the solution offered by LibGC has none of these disadvantages.

Introduction

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

Problem # 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.

Problem # 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).

Problem # 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.

Example:

//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 {
public:
};

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

//main
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;
}</int></int>

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.

History

  • 31st July, 2004: Initial version

License

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.


Written By
Software Developer (Senior)
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFails with error EXC_BAD_ACCESS Pin
Volodymyr Kopytin22-Jun-20 2:58
professionalVolodymyr Kopytin22-Jun-20 2:58 
GeneralLibGC error Pin
caoctaviano25-Jun-09 9:46
caoctaviano25-Jun-09 9:46 
GeneralProblem with libgc Pin
pulkownik1126-Mar-08 23:37
pulkownik1126-Mar-08 23:37 
QuestionA question in reading your code Pin
nightelf19843-Jun-07 21:03
nightelf19843-Jun-07 21:03 
QuestionIs it thread safe? Pin
Aliff9-Oct-04 19:07
Aliff9-Oct-04 19:07 
GeneralAn interesting possible problem Pin
Nathan Holt at EMOM3-Aug-04 7:07
Nathan Holt at EMOM3-Aug-04 7:07 
GeneralRe: An interesting possible problem Pin
Achilleas Margaritis3-Aug-04 23:49
Achilleas Margaritis3-Aug-04 23:49 
GeneralRe: An interesting possible problem Pin
Edwin G. Castro10-Aug-04 8:17
Edwin G. Castro10-Aug-04 8:17 
GeneralMingW32 support. Pin
Achilleas Margaritis3-Aug-04 6:19
Achilleas Margaritis3-Aug-04 6:19 
GeneralGarbage Pin
TW2-Aug-04 16:33
TW2-Aug-04 16:33 
GeneralRe: Garbage Pin
Achilleas Margaritis3-Aug-04 0:45
Achilleas Margaritis3-Aug-04 0:45 
GeneralRe: Garbage Pin
Nemanja Trifunovic3-Aug-04 5:58
Nemanja Trifunovic3-Aug-04 5:58 
GeneralRe: Garbage Pin
Achilleas Margaritis3-Aug-04 6:21
Achilleas Margaritis3-Aug-04 6:21 
GeneralRe: Garbage Pin
Nathan Holt at EMOM3-Aug-04 6:53
Nathan Holt at EMOM3-Aug-04 6:53 
GeneralRe: Garbage Pin
Nemanja Trifunovic3-Aug-04 6:56
Nemanja Trifunovic3-Aug-04 6:56 
GeneralRe: Garbage Pin
Achilleas Margaritis3-Aug-04 23:52
Achilleas Margaritis3-Aug-04 23:52 
GeneralRe: Garbage Pin
Nathan Holt at EMOM4-Aug-04 5:47
Nathan Holt at EMOM4-Aug-04 5:47 
GeneralRe: Garbage Pin
Achilleas Margaritis4-Aug-04 6:44
Achilleas Margaritis4-Aug-04 6:44 
GeneralRe: Garbage Pin
Nathan Holt at EMOM4-Aug-04 7:17
Nathan Holt at EMOM4-Aug-04 7:17 
GeneralRe: Garbage Pin
Achilleas Margaritis5-Aug-04 0:05
Achilleas Margaritis5-Aug-04 0:05 
GeneralRe: Garbage Pin
Nathan Holt at EMOM5-Aug-04 6:44
Nathan Holt at EMOM5-Aug-04 6:44 
GeneralRe: Garbage Pin
Achilleas Margaritis5-Aug-04 10:04
Achilleas Margaritis5-Aug-04 10:04 
GeneralRe: Garbage Pin
Nathan Holt at EMOM6-Aug-04 6:59
Nathan Holt at EMOM6-Aug-04 6:59 
GeneralRe: Garbage Pin
Achilleas Margaritis8-Aug-04 0:40
Achilleas Margaritis8-Aug-04 0:40 
GeneralRe: Garbage Pin
Nathan Holt at EMOM9-Aug-04 6:48
Nathan Holt at EMOM9-Aug-04 6:48 

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.