All garbage collectors use the following protocols for status reporting and statistics gathering.
class GC {
public:
enum GCNotify {
gc_no_notify,
gc_notify_minor_collection,
gc_notify_major_collection,
gc_notify_weak_pointer_collection,
gc_print_collection_time,
gc_print_debugging_info
}
virtual int verbosity() const;
virtual void set_verbosity(int);
virtual ostream& get_console() const;
virtual void set_console(ostream&);
};
class GC {
public:
struct Statistics {
const char * algorithm;
const char * version;
size_t bytes_used;
size_t bytes_managed;
size_t bytes_free;
struct timeval gc_user_time;
struct timeval gc_system_time;
struct timeval total_gc_user_time;
struct timeval total_gc_system_time;
}
virtual Statistics statistics();
};
Each collector has a current verbosity level, which can be set
and reset using the methods set_verbosity(int) and
verbosity() const. The verbosity level is actually a bit
set containing flags of the type GC::GCNotify. A combination
of these options can be used.
gc_no_notify -- no messages at all.
gc_notify_minor_collection -- all minor collections
will be notified by printing a message on the current console.
gc_notify_major_collection -- similarly for all major
collections.
gc_notify_weak_pointer_collection -- notify the user
when weak pointers are being collected.
gc_print_collection_time -- this option will let the
collector to print the time spent during collection (and finalization).
gc_print_debugging_info -- this option will print additional
information such as stack and heap addresses during garbage collection.
The current console of a collector is defaulted to the C++ stream
cerr. It can be set and inspected with the methods
set_console(ostream&) and ostream& get_console() respectively.
For example, the user can redirect all garbage collection messages
to a log file "gc.log" by executing the following during initialization:
ofstream gc_log("gc.log");
GC::get_default_gc().set_console(gc_log);