### 4.3 Adjusting the size of Prolog stacks

GNU Prolog uses several stacks to execute a Prolog program. Each stack has a static size and cannot be dynamically increased during the execution. For each stack there is a default size but the user can define a new size by setting an environment variable. When a GNU Prolog program is run it first consults these variables and if they are not defined uses the default sizes. The following table presents each stack of GNU Prolog with its default size and the name of its associated environment variable:

Stack | Default | Environment | Description
name | size (Kb) | variable |
`local` | 16384 | `LOCALSZ` | control stack (environments and choice-points)
`global` | 32768 | `GLOBALSZ` | heap (compound terms)
`trail` | 16384 | `TRAILSZ` | conditional bindings (bindings to undo at backtracking)
`cstr` | 16384 | `CSTRSZ` | finite domain constraint stack (FD variables and constraints)

In addition, under Windows (since version 1.4.0), registry keys are consulted (key names are the same as environment names). The keys are stored in `HKEY_CURRENT_USER\Software\GnuProlog\`.

If the size of a stack is too small an overflow will occur during the execution. In that case GNU Prolog emits the following error message before stopping:

  `S stack overflow (size: N Kb, environment variable used: E)`

where `S` is the name of the stack, `N` is the current stack size in Kb and `E` the name of the associated environment variable. When such a message occurs it is possible to (re)define the variable `E` with the new size. For instance to allocate Kb to the local stack under a Unix shell use:

  `LOCALSZ=32768; export LOCALSZ` |  | (under `sh` or `bash`)
  `setenv LOCALSZ 32768` |  | (under `csh` or `tcsh`)

This method allows the user to adjust the size of Prolog stacks. However, in some cases it is preferable not to allow the user to modify these sizes. For instance, when providing a stand alone executable whose behavior should be independent of the environment in which it is run. In that case the program should not consult environment variables and the programmer should be able to define new default stack sizes. The GNU Prolog compiler offers this facilities via several command-line options such as `--local-size` or `--fixed-sizes` (section 4.4.3).

Finally note that GNU Prolog stacks are virtually allocated (i.e. use virtual memory). This means that a physical memory page is allocated only when needed (i.e. when an attempt to read/write it occurs). Thus it is possible to define very large stacks. At the execution, only the needed amount of space will be physically allocated.

Copyright (C) 1999-2011 Daniel Diaz Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. More about the copyright