How to clear mxArray ?

2 views (last 30 days)
Gauraang  Khurana
Gauraang Khurana on 5 Jan 2016
Commented: James Tursa on 7 Jan 2016
I have an mxArray which has a matrix. I want to clear this mxArray now and use it for another function. Can I over write it ? If not, then how can I remove the content previously stored in the array ?
I want to save memory.
Code :
mxArray *pplhs[1]; mexCallMATLAB(nlhs,pplhs,nrhs,array,"transpose");
Now this returns a matrix in pplhs. I want to use pplhs again in another mexCallMATLAB function. How to do it ?
Thanks Gauraang Khurana
  1 Comment
Sivakumaran Chandrasekaran
you can over write it.. else, assign another variable for it.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 6 Jan 2016
Edited: James Tursa on 6 Jan 2016
You cannot re-use mxArrays as outputs from the mexCallMATLAB function. The mexCallMATLAB function always allocates a new mxArray for the output(s). There is no way to avoid this. So you just need to accept this overhead if you use mexCallMATLAB. E.g.,
mxArray *pplhs[1];
mexCallMATLAB(nlhs,pplhs,nrhs,array,"transpose");
// do some work and generate a different "array" variable
mexCallMATLAB(nlhs,pplhs,nrhs,array,"another_function"); % <-- Leaks memory
The 2nd mexCallMATLAB call above will leak memory (at least temporarily). That is, the mxArray that is present in pplhs[0] from the 1st mexCallMATLAB call does not get cleared or re-used in any way. The pointer to it is simply lost and overwritten by the 2nd mexCallMATLAB call, hence the memory leak. If the matrices are large and this code is in a loop, you risk running out of memory. Exiting from the mex routine will clear these lost mxArrays because MATLAB provided garbage collection for you, but that is not good programming practice to rely on this. To avoid a memory leak one would have to do this:
mxArray *pplhs[1];
mexCallMATLAB(nlhs,pplhs,nrhs,array,"transpose");
// do some work and generate a different "array" variable
mxDestroyArray(pplhs[0]); // <-- Free the mxArray in pplhs[0]
mexCallMATLAB(nlhs,pplhs,nrhs,array,"another_function"); % <-- Does not leak memory
Bottom line is the only way to re-use mxArrays or memory in a mex routine is to hand code the operations in C/C++ at the memory level ... you cannot use mexCallMATLAB for this purpose. E.g., if you wanted to re-use the mxArray for the transpose result, you would be required to hand-code the transpose code yourself in C/C++ (or maybe call some library function to do it that takes both input and output pointers as arguments, etc).
  2 Comments
Gauraang  Khurana
Gauraang Khurana on 6 Jan 2016
I have just started with mex and I my calculations are huge.I have used mexCallMATLAB for every possible computation like addition, substraction and transpose also. This is mainly because I couldn't understand these functions in a row and column major system. I am not at all familiar with header files. Can you please help me with this. A link or an example would be great. How can I perform any of these operations on a matrix without calling mexCallMATLAB because eventually I have to optimize my code.
Thanks in advance, I could really use your help right now.
James Tursa
James Tursa on 7 Jan 2016
How you speed up m-code or avoid unnecessary memory copies by coding at the mex level depends greatly on specifically what you are doing at the m-file level. So maybe you could open up a new Question with a specific calculation you need to improve for timing or memory usage, and we can provide suggestions on how to do it (or offer an opinion that it can't be done). E.g., Simple operations like adding or multiplying cannot be sped up in a mex routine:
C = A + B;
C = A * B;
The best one could hope for if you were doing either of the above in a mex routine would be to generate code that tied MATLAB m-code for speed. Thus, there is no point in writing example C-code for this.

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!