The SWI-Prolog engine uses three stacks the local stack (also called environment stack) stores the environment frames used to call predicates as well as choice points. The global stack (also called heap) contains terms, floats, strings and large integers. Finally, the trail stack records variable bindings and assignments to support backtracking. Except for available memory, there is no hard limit for the sizes of the stacks.42As of version 9.3.6. Older versions have a hard limit on 32-bit hardware of 128Mb for each stack.
The combined stack size (per thread) has a soft limit implemented by the writeable flag stack_limit or the command line option --stack-limit. Currently the default limit is 1Gb. Considering portability, applications that need to modify the default limits are advised to do so using the Prolog flag stack_limit.
Area name | Description |
local stack | The local stack is used to store the execution environments of procedure invocations. The space for an environment is reclaimed when it fails, exits without leaving choice points, the alternatives are cut off with the !/0 predicate or no choice points have been created since the invocation and the last subclause is started (last call optimisation). |
global stack | The global stack is used to store terms, strings, big integers, rational numbers and floating numbers created during Prolog's execution. Data on this stack is reclaimed by backtracking to a point before the data was created or by garbage collection (provided the data is no longer referenced). |
trail stack | The trail stack is used to store
assignments during execution. Entries on this stack remain alive until
backtracking before the point of creation or the garbage collector
determines they are no longer needed.
As the trail and global stacks are garbage collected together, a small trail can cause an excessive amount of garbage collections. To avoid this, the trail is automatically resized to be at least 1/6th of the size of the global stack. |
With the heap, we refer to the memory area used by malloc() and friends. SWI-Prolog uses the area to store atoms, functors, predicates and their clauses, records and other dynamic data. No limits are imposed on the addresses returned by malloc() and friends.