Can I rely completely on the memory management of MATLAB to free up the memory in my C-MEX routine?

4 views (last 30 days)
Can I rely completely on the memory management of MATLAB to free up the memory in my C-MEX routine?
MATLAB frees up the memory automatically in C-MEX routines. Do I have to explicitly use the mxFree routine to free up the memory allocated? Is there any advantage of using mxFree over completely relying on the memory management of MATLAB? MATLAB documentation says:
It is a good programming practice to deallocate memory just as soon as you are through using it. Doing so generally makes the entire system run more efficiently.
If the memory management engine of MATLAB is going to free up the memory automatically, why is the use of mxFree more efficient?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
If your MEX-routine is small and memory allocation is not done in different scopes, the use of manual memory allocation has no advantage over the automatic memory management. The efficiency of your MEX-routines will be the same even if you do not manually free the memory and rely solely on the memory management utility of MATLAB.
However, if your program is large and you have data manipulation being done in different scopes (for example, functions), opt for manual de-allocation of memory rather than waiting for the complete execution of the program (after which the memory management utility of MATLAB would occur nonetheless). Manual de-allocation makes the job easier for memory management, avoiding the overheads of making a log of all the places where memory is being allocated. For this case, if you are sure that you will not be needing a particular chunk of data, it is advised to de-allocate the memory manually.
  2 Comments
James Tursa
James Tursa on 9 Jan 2017
There is one API function that does not follow the assumptions above, however. The API function mxArrayToString copies the 2-bytes per character string data from an mxArray into a newly allocated memory block as a 1-byte per character C-style string (with appended null character at the end). Unfortunately, this newly allocated memory block IS NOT ON THE GARBAGE COLLECTION LIST ! So you, the programmer, are forced to manually delete this memory block in your mex code or you will have a permanent memory leak, since the MATLAB Memory Manager will not do it for you when the mex routine completes execution. I have discussed this situation with the TMW folks and they have no intention of changing this behavior, so be aware of this if you ever use the mxArrayToString API function.
James Tursa
James Tursa on 12 Jul 2023
Update: As of R2017a, mxArrayToString memory is now on the garbage collection list. You no longer are forced to manually delete it.

Sign in to comment.

More Answers (0)

MathWorks Support

Categories

Find more on Performance and Memory in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!