KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Calculating the minimum stack size
PRODUCT: 4D | VERSION: 2004 | PLATFORM: Mac & Win
Published On: July 7, 2006
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

4th Dimension (4D) allows/requires the developer to specify the stack size for new processes.

The 4D Language reference states the following:

"Process Stack: In stack, you pass the amount of memory allocated for the stack of the process. It is the space in memory used to "pile up" method calls, local variables, parameters in subroutines, and stacked records. It is expressed in bytes; it is recommended to pass at least 64K (around 64,000 bytes), but you can pass more if the process can perform large chain calls (subroutines calling subroutines in cascade). For example, you can pass 200K (around 200000 bytes), if necessary."

The documentation does not specify an upper bound although, given that 4D can address up to only 2GB of memory, the upper bound would in theory be 2GB-(the total memory it takes to run your database).

Of course a stack of, say, 750MB would be excessive and, regardless of the bounds, stack size should be calculated based on the needs of the process. The question is how can the 4D developer determine how much stack is needed for a given process?

There are general rules one can follow when picking stack sizes (the quote above recommends a range of 64KB-200KB) but such rules cannot cover all situations.

There are two techniques a 4D developer can use in order to more finely tune the stack size required for a given process:

1. Design-based calculation
2. Testing-based calculation


Design-Based Calculation:

The 4D developer can leverage their knowledge of the database design in order to more accurately calculate stack space. The Language Reference highlights what information is stored in the stack. Therefore the developer can analyse the design of a given process and calculate the stack required for it.

For example a recursive function for calculating a factorial might have 1 local variables (the product), 1 parameter (the input), and 1 return value (the result). These variables might all be long integers so at least 12 bytes are needed for local variables and parameters. The possible depth of the recursion can be calculated based on an input boundary. Thus the minimum stack size would be 12 bytes times the maximum number of recursions.

This is a contrived example but it demonstrates that it is possible to calculate stack size based on the design of the database.


Testing-Based Calculation:

The 4D Pack plug-in provides a method called "AP AVAILABLE MEMORY" that can be used to check the amount of free stack space for a given process. The 4D developer can use this method to "tune" the stack size a process might need.

The idea is to "exercise" the process in order to get an idea of how much stack memory it uses. The developer needs to explore the boundaries of the process with testing routines that maximize the memory use. The developer should periodically check the free stack space to see how much stack is being used.

Of course for this to work a overly large stack size should be chosen to begin with. Then, through iterative testing, the stack size can be reduced until an acceptable minimum level is reached.