Node:GC Hooks, Next:, Up:Guile Hooks



24.6.5.1 Hooks for Garbage Collection

Whenever Guile performs a garbage collection, it calls the following hooks in the order shown.

scm_before_gc_c_hook C Hook
C hook called at the very start of a garbage collection, after setting scm_gc_running_p to 1, but before entering the GC critical section.

If garbage collection is blocked because scm_block_gc is non-zero, GC exits early soon after calling this hook, and no further hooks will be called.

scm_before_mark_c_hook C Hook
C hook called before beginning the mark phase of garbage collection, after the GC thread has entered a critical section.

scm_before_sweep_c_hook C Hook
C hook called before beginning the sweep phase of garbage collection. This is the same as at the end of the mark phase, since nothing else happens between marking and sweeping.

scm_after_sweep_c_hook C Hook
C hook called after the end of the sweep phase of garbage collection, but while the GC thread is still inside its critical section.

scm_after_gc_c_hook C Hook
C hook called at the very end of a garbage collection, after the GC thread has left its critical section.

after-gc-hook Scheme Hook
Scheme hook with arity 0. This hook is run asynchronously (see Asyncs) soon after the GC has completed and any other events that were deferred during garbage collection have been processed. (Also accessible from C with the name scm_after_gc_hook.)

All the C hooks listed here have type SCM_C_HOOK_NORMAL, are initialized with hook closure data NULL, are are invoked by scm_c_hook_run with call closure data NULL.

The Scheme hook after-gc-hook is particularly useful in conjunction with guardians (see Guardians). Typically, if you are using a guardian, you want to call the guardian after garbage collection to see if any of the objects added to the guardian have been collected. By adding a thunk that performs this call to after-gc-hook, you can ensure that your guardian is tested after every garbage collection cycle.