A classic unsafe runtime error is to free a data structure that is still reachable by active references (or ``dangling pointers''). The error plants a time bomb that explodes later, when the storage is reused. If on the other hand the programmer fails to free records that have become unreachable, the result will be a ``storage leak'' and the computation space will grow without bound. Problems due to dangling pointers and storage leaks tend to persist long after other errors have been found and removed. The only sure way to avoid these problems is the automatic freeing of unreachable storage, or garbage collection.
Modula-3 therefore provides ``traced references'', which are like Modula-2 pointers except that the storage they point to is kept in the ``traced heap'' where it will be freed automatically when all references to it are gone.
Another great benefit of garbage collection is that it simplifies interfaces.
Without garbage collection, an interface must specify whether the client or
the implementation has the responsibility for freeing each allocated
reference, and the conditions under which it is safe to do so. This can swamp
the interface in complexity. For example, Modula-3 supports text strings by a
simple required interface Text
, rather than with a built-in type.
Without garbage collection, this approach would not be nearly as attractive.
New refinements in garbage collection have appeared continually for more than twenty years, but it is still difficult to implement efficiently. For many programs, the programming time saved by simplifying interfaces and eliminating storage leaks and dangling pointers makes garbage collection a bargain, but the lowest levels of a system may not be able to afford it. For example, in SRC's Topaz system, the part of the operating system that manages files and heavy-weight processes relies on garbage collection, but the inner ``nub'' that implements virtual memory and thread context switching does not. Essentially all Topaz application programs rely on garbage collection.
For programs that cannot afford garbage collection, Modula-3 provides a set of reference types that are not traced by the garbage collector. In most other respects, traced and untraced references behave identically.
m3-support@elego.de