6 Miscellaneous Programming Features

6.7 Finalization

Sometimes you need to do more than simply reclaim the space occupied by inaccessible Lisp objects. For instance, the objects might be involved with foreign resources of some kind, such as network connections. Or you might want to write code to operate on the dead objects one final time in order to free up network resources after the objects are no longer needed.

These motives lead to the notion of a finalization queue, which is a list containing dead objects that you are interested in subjecting to further processing. You may request the garbage collector to place objects on these queues by registering them at any time during their lifetime. (Registration depends on the same notion of "pointing to but not making a reference to" that constitutes weak pointers. For finalization, it is crucial that this registration does not constitute a reference with respect to the garbage collector. Otherwise the garbage collector would consider the object still "alive" and would not attempt to recover it.)

The Unique Access Property (i.e., that the pointer to an object that registers it for finalization is the only extant pointer to it, not including pointers within the object that refer to it) guarantees that objects on finalization queues are not referred to anywhere else. Another property, the Principle of Containment, means that if A points to B in a simple tree fashion (without circularities), and both are registered and become dead at the same time, then A will be finalized first and B will be finalized only when A becomes dead and is no longer registered.

If an object is registered in several queues, the order in which the object will appear in the queues is not specified.

6.7.1 - Finalization and Disksave
6.7.2 - Reference pages

The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker