30.  Garbage Collection  

The interpreter supports two garbage collectors: the stop-and-copy garbage collector that was part of older versions of Elk, and a generational, incremental garbage collector.

If generational garbage collection has been selected, Scheme objects surviving two garbage collections will not be touched again until there is only a certain amount of memory left on the heap, triggering a full garbage collection. Particularly in applications with large amounts of Scheme code or constant data, partial garbage collections run much faster than full garbage collections. In contrast to the stop-and-copy garbage collector, the generational garbage collector is not limited to a pre-allocated amount of heap; it will expand the heap in steps of 1 MB if the free space left after a full garbage collection falls below a certain amount.

Another feature of the generational garbage collector (available on some platforms only) is the ability to do incremental garbage collection. Starting a garbage collection does not interrupt the application until the garbage collector is done. Instead, the collector returns control to the application almost immediately. To synchronize between the garbage collection and the running application, the code makes use of the mprotect system call.

(garbage-collect-status strategy mode)

garbage-collect-status is used to select a garbage collector and an optional, garbage collector specific mode of operation, and to query the currently enabled garbage collector and mode.

strategy is a symbol identifying a garbage collector. Permitted values are stop-and-copy and generational (future version of Elk may support additional garbage collectors). The optional mode argument may be specified if the strategy argument is equal to generational. Currently, only the symbol incremental may be used for the mode argument to enable incremental garbage collection.

The current version of the interpreter does not support dynamic switching between the stop-and-copy and the generational, incremental garbage collector at runtime. Instead, a garbage collector has to be selected at compile time (by setting the generational_gc variable in the installation's site file to either yes or no). Thus, garbage-collect-status can currently only be used to query the garbage collector and, if the generational, incremental garbage collector has been selected, to enable and disable incremental garbage collection (this restriction may be removed in future versions).

garbage-collect-status returns a list of symbols indicating the currently enabled garbage collector and mode. This list resembles the arguments to garbage-collect-status, i.e. the first element of the list one of the symbols stop-and-copy and generational, and an optional, second symbol (incremental) may be present if the first symbol is equal to generational.

If garbage-collect-status is invoked with no arguments, or if the desired garbage collector or mode of operation cannot be enabled (either because selection of a strategy at runtime is not supported, of because the mode of operation cannot be supported), the primitive just returns the currently active strategy and mode.


Causes a garbage collection. Even if incremental garbage collection has been enabled, collect always performs a full garbage collection run.


This primitive is only present if the generational garbage collector has been selected. An error is signaled if collect-incremental is invoked and incremental garbage collection has not been enabled, i.e. if a call to garbage-collect-status would return the list (generational).

collect-incremental starts an incremental garbage collection and then returns immediately. If an incremental garbage collection is already in progress, collect-incremental triggers one incremental garbage collection step, i.e. scans a few more pages of memory, and then returns immediately. The primitive returns true if the incremental garbage collection has been finished, false otherwise.

If incremental garbage collection is disabled by a call to (garbage-collect-status 'generational) while an incremental garbage collection run is in progress, the next call to collect-incremental finishes the incremental garbage collection run and returns #t; further calls to collect-incremental will signal an error.


This variable is defined in the global environment. If the value of garbage-collect-notify? is true, a message indicating the amount of free memory on the heap and the size of the heap are displayed whenever a stop-and-copy garbage collection is performed. If the generational, incremental garbage collector has been enabled, the amount of reclaimed memory is displayed on each garbage collection run, and a message is displayed each time the heap is expanded by the garbage collector. garbage-collect-notify? is bound to #t initially.

Markup created by unroff 1.0,    September 24, 1996,    net@informatik.uni-bremen.de