Next: Metaobject Protocol, Up: Beyond the ANSI Standard

### 7.1 Garbage Collection

SBCL provides additional garbage collection functionality not specified by ANSI. Weak pointers allow references to objects to be maintained without keeping them from being garbage collected, and “finalization” hooks are available to cause code to be executed when an object has been garbage collected. Additionally users can specify their own cleanup actions to be executed with garbage collection. See also `make-hash-table` for information on weak hash tables.

— Function: sb-ext:finalize object function &key dont-save

> Arrange for the designated `function` to be called when there are no more references to `object`, including references in `function` itself.

> If `dont-save` is true, the finalizer will be cancelled when `save-lisp-and-die` is called: this is useful for finalizers deallocating system memory, which might otherwise be called with addresses from the old image.

> In a multithreaded environment `function` may be called in any thread. In both single and multithreaded environments `function` may be called in any dynamic scope: consequences are unspecified if `function` is not fully re-entrant.

> Errors from `function` are handled and cause a `warning` to be signalled in whichever thread the `function` was called in.

> Examples:

>                 ;;; good (assumes RELEASE-HANDLE is re-entrant)
>                 (let* ((handle (get-handle))
>                        (object (make-object handle)))
>                  (finalize object (lambda () (release-handle handle)))
>                  object)

>                 ;;; bad, finalizer refers to object being finalized, causing
>                 ;;; it to be retained indefinitely
>                 (let* ((handle (get-handle))
>                        (object (make-object handle)))
>                   (finalize object (lambda () (release-handle (object-handle object)))))

>                 ;;; bad, not re-entrant
>                 (defvar *rec* nil)

>                 (defun oops ()
>                  (when *rec*
>                    (error "recursive OOPS"))
>                  (let ((*rec* t))
>                    (gc))) ; or just cons enough to cause one

>                 (progn
>                   (finalize "oops" #'oops)
>                   (oops)) ; causes GC and re-entry to #'oops due to the finalizer
>                           ; -> ERROR, caught, WARNING signalled

— Function: sb-ext:cancel-finalization object

> Cancel any finalization for `object`.

— Function: sb-ext:make-weak-pointer object

> Allocate and return a weak pointer which points to `object`.

— Function: sb-ext:weak-pointer-value weak-pointer

> If `weak-pointer` is valid, return the value of `weak-pointer` and `t`. If the referent of `weak-pointer` has been garbage collected, returns the values `nil` and `nil`.

— Variable: sb-ext:*after-gc-hooks*

> Called after each garbage collection, except for garbage collections triggered during thread exits. In a multithreaded environment these hooks may run in any thread.