next up previous contents
Next: Making the Most of Up: Recipe for Use Previous: On the Stack

On the Heap

In this mode, you allocate the garbage collector on the heap, & give it to Giggle by calling setGc on it. After that, Giggle owns the Garbage Collector & will tell it when to shutdown. Giggle will also delete it.

So your program looks like this:

    using CyberTiggyr::giggle::DefaultCollectorType;
    using CyberTiggyr::giggle::gc;
    using CyberTiggyr::giggle::setGc;

    int
    main (int argc, char *argv[])
    {
        setGc (new DefaultCollectorType);
        run_my_app ();
        return 0;
    }

Some rules include:

  1. You must create your Garbage Collector on the heap.

  2. Once you give the Garbage Collector to Giggle, you must not tell it to shutdown & you must not delete it, ever.

  3. You can use multiple collectors just by creating a new collector & calling setGc on it. Giggle will shutdown & delete the previous Garbage Collector, if there was one. The main reasons for doing this is to compare performances of different Garbage Collectors in the same program, one at a time.

  4. If you allow a bad_alloc to terminate the program when there is an active Garbage Collector, Giggle will tell the collector to shutdown & then will delete the collector.

A disadvantage of this system is that it reduces your options of where to allocate your Garbage Collector. You must allocate it on the heap, but with just that one disadvantage, you get a lot of benefits from simplification. You just create your Garbage Collector with new, hand it to setGc, & forget about it. Giggle takes care of everything else.

A disadvantage is portability. During program shutdown, Giggle would shutdown & then delete the Garbage Collector. Is that portable? Is it possible that, on some systems, the heap memory manager would have shutdown by the time Giggle figured out the program was exiting?


next up previous contents
Next: Making the Most of Up: Recipe for Use Previous: On the Stack
Gene Michael Stover
2002-04-28