Calculation of the Stack Usage
The stack usage per POU is displayed in the Task Configuration object on the Stack Usage tab.
The stack usage is calculated statically by CODESYS using the static call stack.
The following is included in the calculation:
The calculation starts with the implicit POU for the entry of the task.
For each IEC program call (e.g.
PLC_PRG
), the stack usage of each IEC POU called is calculated recursively. The largest possible stack usage of a POU is added to its own local stack usage in order to determine the maximum stack usage of the POU.For dynamic calls, the stack usage is calculated as follows: The stack usage of all possible implementations is determined and the largest stack usage is assumed. This means that all possible implementations are taken into account for interface methods and all possible overrides for function block methods.
The value specified by the target device is assumed for external calls. If the device has not specified a value, then 0 is assumed.
In the case of recursion, the calculation is aborted. The value of the type attribute pragma estimated-stack-usage is applied for the recursively called code. If no value is specified,then 0 is assumed.
The stack usage is therefore only an estimate. It may be too high because the assumed lowest call path is never run through at runtime. However, it can also be too low because recursions or external calls are estimated unfavorably.

Displaying the stack usage
Double-click the Task Configuration object.
The task configuration editor opens.
Click Build → Generate Code command.
Click the Stack Usage tab.
The information on the tab is updated.
Under Task/Maximum Call Stack, the POUs called in sequence appear as assumed in the maximum call stack. The calculated stack size for the POU is displayed in the Stack Size column.
If the stack size is displayed in red, then there might be a stack overflow in the respective POU. You can recognize the POU where you should make changes. This is the first POU marked in red in the call stack.
