Home Contents Index Summary Previous Next

4.42 Memory Management

Note: limit_stack/2 and trim_stacks/0 have no effect on machines that do not offer dynamic stack expansion. On these machines these predicates simply succeed to improve portability.

garbage_collect
Invoke the global- and trail stack garbage collector. Normally the garbage collector is invoked automatically if necessary. Explicit invocation might be useful to reduce the need for garbage collections in time critical segments of the code. After the garbage collection trim_stacks/0 is invoked to release the collected memory resources.

garbage_collect_atoms
Reclaim unused atoms. Normally invoked after agc_margin (a prolog flag) atoms have been created. On multi-threaded versions the actual collection is delayed until there there are no threads performing normal garbage collection. In this case garbage_collect_atoms/0 returns immediately. Note this implies there is no guarantee it will ever happen as there may always be threads performing garbage collection.

limit_stack(+Key, +Kbytes)
Limit one of the stack areas to the specified value. Key is one of local, global or trail. The limit is an integer, expressing the desired stack limit in K bytes. If the desired limit is smaller than the currently used value, the limit is set to the nearest legal value above the currently used value. If the desired value is larger than the maximum, the maximum is taken. Finally, if the desired value is either 0 or the atom unlimited the limit is set to its maximum. The maximum and initial limit is determined by the command line options -L, -G and -T.

trim_stacks
Release stack memory resources that are not in use at this moment, returning them to the operating system. Trim stack is a relatively cheap call. It can be used to release memory resources in a backtracking loop, where the iterations require typically seconds of execution time and very different, potentially large, amounts of stack space. Such a loop should be written as follows:


loop :-
        generator,
            trim_stacks,
            potentially_expensive_operation,
        stop_condition, !.

The prolog top level loop is written this way, reclaiming memory resources after every user query.

stack_parameter(+Stack, +Key, -Old, +New)
Query/set a parameter for the runtime stacks. Stack is one of local, global, trail or argument. The table below describes the Key/Value pairs. Old is first unified with the current value.

limit Maximum size of the stack in bytes
min_free Minimum free space at entry of foreign predicate

This predicate is currently only available on versions that use the stack-shifter to enlarge the runtime stacks when necessary. It's definition is subject to change.