Main Content

Set Dynamic Memory Allocation Threshold

This example shows how to specify a dynamic memory allocation threshold for variable-size arrays. Dynamic memory allocation optimizes storage requirements for variable-size arrays, but causes slower execution of generated code. Instead of disabling dynamic memory allocation for all variable-size arrays, you can disable dynamic memory allocation for arrays less than a certain size.

Specify this threshold when you want to:

  • Disable dynamic memory allocation for smaller arrays. For smaller arrays, static memory allocation can speed up generated code. Static memory allocation can lead to unused storage space. However, 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.

Set Dynamic Memory Allocation Threshold Using the MATLAB Coder App

  1. To open the Generate dialog box, on the Generate Code page, click the Generate arrow .

  2. Click More Settings.

  3. On the Memory tab, select Enable dynamic memory allocation.

  4. Set Dynamic memory allocation threshold to the value that you want.

    The Dynamic memory allocation threshold value is measured in bytes. Based on information from the target hardware settings, the software estimates the size of the array that a certain value of DynamicMemoryAllocationThreshold can accommodate. This estimate excludes possible C compiler optimizations such as putting variables in registers.

Set Dynamic Memory Allocation Threshold at the Command Line

  1. Create a configuration object for code generation. Use coder.config with arguments 'lib','dll', or 'exe' (depending on your requirements). For example:

    cfg = coder.config('lib');
  2. Set EnableDynamicMemoryAllocation to true.

    cfg.EnableDynamicMemoryAllocation=true;
  3. Set the property, DynamicMemoryAllocationThreshold, to the value that you want.

    cfg.DynamicMemoryAllocationThreshold = 40000;

    The value stored in DynamicMemoryAllocationThreshold is measured in bytes. Based on information from the target hardware settings, the software estimates the size of the array that a certain value of DynamicMemoryAllocationThreshold can accommodate. This estimate excludes possible C compiler optimizations such as putting variables in registers.

Related Examples

More About