Contenuto principale

Control Dynamic Memory Allocation in Generated Code

During code generation, the code generator statically or dynamically allocates memory for arrays in the MATLAB® code. Dynamically allocating memory can cause the generated code to execute more slowly, but can optimize the storage requirements. You can choose how to optimize your generated code by specifying when the code generator uses dynamic memory allocation.

By default, the code generator balances speed and storage requirements. It:

  • Dynamically allocates memory for unbounded variable-size arrays.

  • Dynamically allocates memory for bounded variable-size arrays whose maximum size is greater than 64 kilobytes.

  • Statically allocates memory for bounded variable-size arrays whose maximum size is less than 64 kilobytes.

  • Statically allocates memory for fixed-size arrays.

To optimize the speed and memory usage of the generated code, you can modify dynamic memory allocation settings to disable dynamic memory allocation for all arrays, change the dynamic memory allocation threshold, or enable dynamic memory allocation for fixed-size arrays.

To learn more about fixed- and variable-size arrays in code generation, see Generate Code for Variable-Size Arrays. You can learn whether an array dimension in the generated code is fixed size, bounded variable size, or unbounded by inspecting the code generation report. See Code Generation Reports.

Disable Dynamic Memory Allocation for All Arrays

Dynamically allocating memory can cause the generated code to execute more slowly, but can improve the storage requirements. If you want to optimize your code for speed, you can disable dynamically memory allocation. You can only disable dynamic memory allocation if your MATLAB code does not contain unbounded arrays. If you disable dynamic memory allocation and your code contains unbounded arrays, code generation fails.

An array is unbounded when the code generator cannot determine an upper bound for one or more array dimensions. For example, the code generator cannot determine an upper bound for an array dimension when:

  • You specify the size of the dimension by using a run-time value.

  • You declare the array dimension as variable size by using coder.varsize, and you do not specify an explicit upper bound. You then expand the array dimension inside a loop by using concatenation, and the number of loop iterations is a run-time value.

  • You use the reshape function on the array, and at least one of the size arguments to the reshape function is a run-time value.

  • You grow a vector by using end+1 in a loop, and the number of loop iterations is a run-time value.

You can avoid unbounded variable-size arrays by explicitly setting an upper bound for variable-size arrays. See Specify Upper Bounds for Variable-Size Arrays in Generated Code.

To disable dynamic memory allocation, use one of these approaches:

  • In a MATLAB Coder™ code configuration object, set the EnableDynamicMemoryAllocation property to false.

  • In the MATLAB Coder Code Generation Settings dialog box, clear the Enable dynamic memory allocation check box.

Configure Dynamic Memory Allocation Threshold

The code generator uses dynamic memory allocation for arrays larger than the dynamic memory allocation threshold. By default, the threshold is 64 kilobytes. You can modify the default dynamic memory allocation threshold to balance the memory and performance requirements of your hardware and application. For example:

  • If the speed of the generated code is more important than storage efficiency, you can increase the threshold to use static memory allocation for more arrays. Using static memory allocation can speed up generated code. However, static memory allocation can lead to unused storage space.

  • If storage efficiency is more important that the speed of the generated code, you can decrease the threshold to use dynamic memory allocation for more arrays. When you use dynamic memory allocation, you can significantly reduce storage requirements. However, dynamic memory allocation can reduce the speed of the generated code. You can decide that the performance impact is not a significant concern for arrays larger than the threshold.

  • If storage efficiency is critical, you can enable dynamic memory allocation for all arrays by setting the threshold to 0.

If dynamic memory allocation is enabled, you can modify the dynamic memory allocation threshold by using one of these approaches:

  • In a MATLAB Coder code configuration object, set a value for the DynamicMemoryAllocationThreshold property.

  • In the MATLAB Coder Code Generation Settings dialog box, enter a value for the Dynamic memory allocation threshold parameter.

Enable Dynamic Memory Allocation for Fixed-Size Arrays

By default, the code generator allocates memory for fixed-size arrays statically during code generation. If the MATLAB code includes large fixed-size arrays, static allocation of memory for these arrays can be inefficient or can even exceed the limits of the stack. If your application must run on a target with limited stack memory, such as an embedded target, consider enabling dynamic memory allocation for fixed-size arrays larger than the dynamic memory allocation threshold.

If dynamic memory allocation is enabled, you can enable dynamic memory allocation for fixed-size arrays by using one of these approaches:

When you enable this parameter and generate code for fixed-size arrays, the code generator statically allocates memory for arrays that are smaller than the Dynamic memory allocation threshold and dynamically allocates memory for arrays that are larger than this threshold.

See Also

| | (Fixed-Point Designer)

Topics