I have 2 questions about boehm-gc.
When GC collects garbage-object, GC free memory without calling destructor although the object has destructor. I found GC calls "finailzer", but I don't know how to register it... How can I do?
When GC collect garbage, GC seems not to call free() (or other memory free function). It seems that GC doesn't free garbage, but put it to GC's memory pool and use pool at next allocation. GC frees the memory pool at idle time? If not, can I say to GC "please free memory pool"?
PS. I can't find boehm-gc reference.. Could you tell me where the reference is?
If you need more reference than is provided in the
gc.h
header file, then you probably should read up on garbage collectors before going any further.Re you question, the
gc.h
header has what you need:So you define a callback:
Create its environment (if any; otherwise just pass NULL):
Then register it on a newly allocated object:
Now
my_callback
will be called with your environment record when this particular object is collected.As to your question 2, you're missing the point. Boehm GC replaces malloc/new and free and manages its own memory arena. It normally decides on its own when to do a collection. This is typically when most of the arena has been used up. Garbage collection identifies blocks that are free, hence eligible for reallocation. As the API notes clearly say, you can force a collection and force-free objects, but these are typically not necessary.