Why does my MATLAB C Math Library application run progressively slower?

1 view (last 30 days)
I am writing a program that executes a loop and must run very quickly (each loop must be under
0.001 sec). The slowest part of a function is by far the line:
mlfAssign(&A, mlfDoubleMatrix(numSpikes, 4, listSpikes, NULL));
This line takes longer and longer each time the function is called.
The context of the function call is as follows:
func()
{
mxArray *A;
double listSpikes[..]
...
//Not inside a loop
mlfAssign(&A, mlfDoubleMatrix(numSpikes, 4, listSpikes, NULL));
...
mxDestroyArray(A);
}

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
It is quite likely that the problem you are experiencing is due to memory leaks in your code. Therefore, you can either use the MATLAB C Math Library's automated memory management feature, or use the explicit memory management.
The explicit memory management requires more account keeping. Each time you allocate memory for an mxArray variable (e.g., when you use the mlfDoubleMatrix routine) you have to destroy the array using mxDestroyArray function. Therefore, if the allocation is done in a loop, the mxDestroyArray should also be called in the loop.
Furthermore, you will have to use the assignment operator "=" to assign values to an mxArray under explicit memory management otherwise you create a memory leak.
With the automated memory management , you have to make sure that:
  • the mxArray is initialized
  • the automated memory management feature is turned on by calling the mlfEnterNewContext and mlfRestorePreviousContext function pair.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!