Control Memory Allocation for Variable-Size Arrays in a MATLAB Function Block
Dynamic memory allocation allocates memory on the heap as needed at run time, instead of allocating memory statically on the stack. You can use dynamic memory allocation for arrays inside a MATLAB Function block.
You cannot use dynamic memory allocation for:
Parameters. Parameters must be fixed-size.
Discrete state properties of System objects associated with a MATLAB System block.
Dynamic memory allocation is beneficial when:
You do not know the upper bound of an array.
You do not want to allocate memory on the stack for large arrays.
Dynamic memory allocation and the freeing of this memory can result in slower execution of the generated code. To control the use of dynamic memory allocation for variable-size arrays in a MATLAB Function block, you can:
Provide Upper Bounds for Variable-Size Arrays
For an unbounded variable-size array, the code generator allocates memory dynamically on the heap. For a bounded variable-size array, if the size, in bytes, is less than the dynamic memory allocation threshold, the code generator allocates memory statically on the stack. To avoid dynamic memory allocation, provide upper bounds for the array dimensions so that the size of the array, in bytes, is less than the dynamic memory allocation threshold. See Specify Upper Bounds for Variable-Size Arrays.
Disable Dynamic Memory Allocation for MATLAB Function Blocks
By default, dynamic memory allocation for MATLAB Function blocks is enabled for GRT-based targets and disabled for ERT-based targets. To change the setting, in the Configuration Parameters dialog box, clear or select Dynamic memory allocation in MATLAB functions. This parameter specifies whether to use dynamic memory allocation for MATLAB® code in MATLAB Function blocks, Stateflow® charts, and MATLAB System blocks in simulation and for code generation.
If you disable dynamic memory allocation, you must provide upper bounds for variable-size arrays.
Modify the Dynamic Memory Allocation Threshold
Instead of disabling dynamic memory allocation for all variable-size arrays, you can use the dynamic memory allocation threshold to specify when the code generator uses dynamic memory allocation.
Use the dynamic memory allocation threshold to:
Disable dynamic memory allocation for smaller arrays. For smaller arrays, static memory allocation can speed up generated code. However, static memory allocation can lead to unused storage space. You can decide that the unused storage space is not a significant consideration for smaller arrays.
Enable dynamic memory allocation for larger arrays. For larger arrays, when you use dynamic memory allocation, you can significantly reduce storage requirements.
The default value of the dynamic memory allocation threshold is 64 kilobytes. To change the threshold, in the Configuration Parameters dialog box, set the Dynamic memory allocation threshold in MATLAB functions parameter. This parameter specifies the threshold, in bytes, above which memory is allocated dynamically for variable-size arrays in MATLAB code in MATLAB Function blocks, Stateflow charts, and MATLAB System blocks in simulation and code generation.
To use dynamic memory allocation for all
variable-size arrays, set the threshold to 0
.