Documentation

Memory Management and Cleanup

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 the MATLAB 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 calling mclInitializeApplication.

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 when mxDestroyArray 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 to mxDestroyArray will no longer be valid.

For more information about mclInitializeApplication and mclTerminateApplication, see Call a C Shared Library.

For more information about mxArray, see C Matrix API (MATLAB).