I’m currently coding a MEX file that needs to run as quickly as possible and is called multiple times by MATLAB within a separate .m file.The MEX file creates and uses a number of variables within C (most of these are primarily double arrays).
I’m wondering whether there will be a speedup associated in deleting / freeing up the variables that I only use within the MEX file. As far as I know, MATLAB already does this though I’ve also heard that some people prefer to free those arrays themselves.
You should cleanup the arrays you allocate yourself. It is true that the MATLAB memory manager will do that for you, but depending on the implementation of the memory manager it may not be very deterministic. While you won’t notice any speed improvements from doing this, there will most likely be memory usage improvements.
Also, MATLAB only does garbage collection for stuff created using
mxMallocandmxCalloc. If you use plainmallocandcallocand don’tfreethe allocated memory you are causing a leak.