Memory Management

Memory management is a complex issue for ProXL. In C, programmers are used to having to keep track of their resources; in Prolog, however, resource reclamation is done by the Prolog garbage collector.

Unfortunately, it's not that simple. The X Window System does not make this possible. For example, there is no way to tell whether or not a given colormap is used by any windows, nor is there a way to tell the X server that a colormap should be reclaimed when no more windows require it. The only function provided by X actually destroys the colormap immediately, and any windows that use that colormap find themselves without any colormap.

Fonts are another matter. Since the only X resource that can refer to a font is a GC, and since ProXL GCs are not shared with other languages or other processes, ProXL can be careful about freeing a font that is relied on by some GC.

There is another difficulty, though: ProXL has no way of knowing whether or not the user's Prolog code is holding onto a font explicitly. For example, the user may assert it into the Prolog database, or pass it around as an argument throughout his program. To Prolog, this font looks just like any other term; it has no way of knowing that it represents a foreign term. This means that the Prolog garbage collector cannot collect foreign terms.

All this means that ProXL has two different mechanisms for freeing resources. Most resources can only be freed by explicit action, and the resource is freed immediately (the next time the output buffer is flushed). GCs and fonts are instead released. Releasing an X resource means that (you are proclaiming that) Prolog does not have any references to the resource. When no X resources refer to the released resource, it will be freed. Fonts can only be referred to by GCs. GCs can be referred to by drawables.