Search code examples
matlabmemory-managementgarbage-collection

MATLAB takes a long time after last line of a function


I have a function that's taking a long time to run. When I profile it, I find that over half the time (26 out of 50 seconds) is not accounted for in the line by line timing breakdown, and I can show that the time is spent after the function finishes running but before it returns control by the following method:

ts1 = tic;
disp ('calling function');
functionCall(args);
disp (['control returned to caller - ', num2str(toc(ts1))]); 

The first line of the function I call is ts2 = tic, and the last line is

disp (['last line of function- ', num2str(toc(ts2))]);

The result is

calling function

last line of function - 24.0043

control returned to caller - 49.857

Poking around on the interwebs, I think this is a symptom of the way MATLAB manages memory. It deallocates on function returns, and sometimes this takes a long time. The function does allocate some large (~1 million element) arrays. It also works with handles, but does not create any new handle objects or store handles explicitly. My questions are:

  1. Is this definitely a memory management problem?
  2. Is there any systematic way to diagnose what causes a problem in this function, as opposed to others which return quickly?
  3. Are there general tips for reducing the amount of time MATLAB spends cleaning up on a function exit?

Solution

  • I discovered a fix to my specific problem that may be applicable in general.

    The function that was taking a long time to exit was called on a basic object that contained a vector of handle objects. When I changed the definition of the basic object to extend handle, I eliminated the lag on the close of the function.

    What I believe was happening is this: When I passed the basic object to my function, it created a copy of that object (MATLAB is pass by value by default). This doesn't take a lot of time, but when the function exited, it destroyed the object copy, which caused it to look through the vector of handle objects to make sure there weren't any orphans that needed to be cleaned up. I believe it is this operation that was taking MATLAB a long time.

    When I changed the object I was passing to a handle, no copy was made in the function workspace, so no cleanup of the object was required at the end.

    This suggests a general rule to me:

    If a function is taking a long time to clean up its workspace on exiting and you are passing a lot of data or complex structures by value, try encapsulating the arguments to that function in a handle object

    This will avoid duplication and hence time consuming cleanup on exit. The downside is that your function can now unexpectedly change your inputs, because MATLAB doesn't have the ability to declare an argument const, as in c++.