4.41 Memory Management

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.
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.
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 :-
        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.