Main Content

Customize Stack Space Allocation

Your application might be constrained by limited memory. Controlling the maximum allowable size for the stack is one way to modify whether data is defined as local or global in the generated code. You can limit the use of stack space by specifying a positive integer value for the Maximum stack size (bytes) parameter, on the Optimization pane of the Configuration parameter dialog box. Specifying the maximum allowable stack size provides control over the number of local and global variables in the generated code. Specifically, lowering the maximum stack size might generate more variables into global structures. The number of local and global variables helps determine the required amount of stack space for execution of the generated code. If you have an Embedded Coder® license, you can track the number and size of global variables in your code using Static Code Metrics.

If your model contains a variable that is larger than 4096 bytes, the code generator implements it in global memory by default. You can increase the size of variables that the code generator places in local memory by changing the value of the TLC variable MaxStackVariableSize. You can change this value by typing the following command in MATLAB® Command Window:

set_param(modelName,'TLCOptions','-aMaxStackVariableSize=N')

The default setting for Maximum stack size (bytes) is Inherit from target. In this case, the value of the maximum stack size is the smaller value of the following: the default value set by the code generator (200,000 bytes) or the value of the TLC variable MaxStackSize found in the system target file (ert.tlc).

To specify a smaller stack size for your application, select the Specify a value option of the Maximum stack size (bytes) parameter and enter a positive integer value. To specify a smaller stack size at the command line, use:

set_param(model_name, 'MaxStackSize', 65000);

Note

For overall executable stack usage metrics, you might want to do a target-specific measurement, such as using runtime (empirical) analysis or static (code path) analysis with object code.

It is recommended that you use the Maximum stack size (bytes) parameter to control stack space allocation instead of modifying the TLC variable, MaxStackSize, in the system target file. However, a target author might want to set the TLC variable, MaxStackSize, for a target. To set MaxStackSize, use assign statements in the system target file (ert.tlc), as in the following example.

%assign MaxStackSize = 4096

Write your %assign statements in the Configure RTW code generation settings section of the system target file. The %assign statement is described in Target Language Compiler.

See Also

Related Topics