Memory Management and Cleanup - MATLAB & Simulink (original) (raw)

Main Content

Overview

Generated C++ code provides consistent garbage collection via the object destructors and the MATLABĀ® Runtime's internal memory manager optimizes to avoid heap fragmentation.

If memory constraints are still present on your system, try preallocating arrays in MATLAB. This will reduce the number of calls to the memory manager, and the degree to which the heap fragments.

Passing mxArrays to Shared Libraries

When an mxArray is created in an application which uses theMATLAB Runtime, it is created in the managed memory space of the MATLAB Runtime.

Therefore, it is very important that you never create mxArrays (or call any other MATLAB function) before callingmclInitializeApplication.

It is safe to call mxDestroyArray when you no longer need a particular mxArray in your code, even when the input has been assigned to a persistent or global variable in MATLAB. MATLAB uses reference counting to ensure that whenmxDestroyArray is called, if another reference to the underlying data still exists, the memory will not be freed. Even if the underlying memory is not freed, the mxArray passed tomxDestroyArray will no longer be valid.

For more information about mclInitializeApplication andmclTerminateApplication, see Call MATLAB Compiler SDK API Functions from C/C++.

For more information about mxArray, see C Matrix API.