4.41 Memory Management

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

set_prolog_stack(+Stack, +Key, +Value)
Set a parameter for a runtime stack. Stack is one of local, global, trail or argument. The table below describes the Key/Value pairs.
low
Do not perform GC below this amount (bytes).
factor
Run next GC if memory exceeds max(low,checked) × factor.
min_free
Enlarge stack if free memory is below this value (bytes). This option is only provided if the system is compiled to support the stack-shifter; otherwise it is silently ignored.

Not all flags have impact for all stacks and the current implementation lacks proper checking for sensible values. Use with extreme care.