KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Process Stack Size: One size does not fit all
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: August 12, 2009

When declaring a New Process it is very memory inefficient to always declare a one size fits all stack size. It is important to consider the purpose of a process when declaring a new process.

The process stack is the space in memory used to "pile up" method calls, local variables, parameters in subroutines, and stacked records. As documented in other Tech Tips, 4D does some self preservation when executing the command New Process. It has an absolute minimum stack size and a default stack size. See KB Tech Tip "What is the smallest stack size 4D allows?" and "What is 4D's minimum recommended stack size?".

4D v11 SQL never gives you exactly what you are asking for in a stack size. It gives what is asked for plus some additional space for its own use. What is allocated will be between 1.6 and 2 times what is requested. The larger the request, the smaller the multiple in how much is allocated.

A process that is going to do minimal work with few variables, does not call other methods, and does not push and pop records requires very little stack size. A process with a lot of variables, arrays, method calls, etc. will require a larger stack. The best way to become familiar with how much memory you should assign a process is via trial and error. The Tech Tip "What is the minimum stack size for my process?" outlines how to get used to what stack size your process requires. In general it is a good idea to be familiar with what stack size different processes (or types of processes) need before deciding if a process should be tiny, small, default, or large as shown in the following code.

The method below can be called from the stack size parameter to declare the stack size desired for the process.

  `Sample Method: SHELL_StackSize
  `Parameters:
  `(Optional) TEXT: the task size; tiny, small, default, or large
  `Return Value:
  `Longint: the stack size


C_LONGINT($0;$StackSize_L)
C_TEXT($Option_T;$1)
C_LONGINT($Ndx;$Platform_L)

$Option_T:=""
$Ndx:=Count parameters
If ($Ndx>0)
  $Option_T:=$1
end if

Case of
  : (($Option_T="tiny") | ($Option_T="min") | ($Option_T="minimum"))
      $StackSize_L:=1 ` 4D allocates 53 KB on Windows and 148 KB on Mac OSX

  : ($Option_T="small")
      $StackSize_L:=128*1024 ` 4D allocates 165 KB on Windows and 316 KB on Mac OSX

  : (($Option_T="") | ($Option_T="default"))
      $StackSize_L:=0 ` 4D allocates 549 KB on Windows and 892 KB on Mac OSX

  : ($Option_T="large")
      $StackSize_L:=640*1024 ` 4D allocates 677 KB on Windows and 1,084 KB on Mac OSX

  Else
      $StackSize_L:=0 ` 4D allocates 549 KB on Windows and 892 KB on Mac OSX

End case

$0:=$StackSize_L


Below is a snippet of code from a method that will automatically launch itself into a new process if a process of the given name does not already exist. Note that in this example it launches with a different size stack based on whether or not a parameter is passed.

C_TEXT($MethodName_T)
$MethodName_T:=Current method name

C_POINTER($TableName_aP;$1)
C_LONGINT($Ndx;$procState_L;$procTime_L)
C_TEXT($procName_T)

$Ndx:=Count parameters
If ($Ndx=1)
    $TableName_aP:=$1
End if

PROCESS PROPERTIES(Current process;$procName_T;$procState_L;$procTime_L)
If ($procName_T#$MethodName_T)
    If ($Ndx=1)
        $Ndx:=New Process($MethodName_T;SHELL_StackSize;$MethodName_T;$TableName_aP)
    Else
        $Ndx:=New Process($MethodName_T;SHELL_StackSize("min");$MethodName_T)
    End if

Else
  `Do some work in a new process
End if

Commented by Atanas Atanassov on August 14, 2009 at 2:17 PM
See also technical tips:

http://kb.4d.com/search/assetid=75836
Commented by Atanas Atanassov on August 14, 2009 at 2:16 PM
http://kb.4d.com/search/assetid=75843
Commented by Atanas Atanassov on August 14, 2009 at 11:49 AM
http://kb.4d.com/search/assetid=75844