Friday, September 14, 2012

Memory Leak in Matlab (multi-dimensional array passing into a function)

First of all, Memory Leak in Matlab maybe misleading.

There is a bug (well I think it is a bug) in Matlab, where memory garbage collection is not done quickly enough, which can result in accumulation of non-usable memory space.

This accumulation of non-usable memory can lead to crashes of the code you are running in Matlab. I have confirm this issue at least in Matlab 2009a. I don't know if this is still an issue for higher Matlab versions. It is likely that it is still an issue and style of coding in Matlab to avoid this issue may be preferred.


Let's look at a function "y = myfunc( x )" which is called multiple times (many many many times) in the whole algorithm. myfunc() take in a matrix and does some processing on it (without changing the value of the input matrix itself).

e.g.

-----problematic function-----------------
function [ y ] =  myfunc( x )

% take x and process something to get results
w = rand( 100, 1 );
y = w'*x*w;

end % end of function
------------------------------------------


Lets assume the input of the function belongs to a higher dimensional array. For example if input size of the function is [ 100, 100 ] dimensional array, the actual array size is [ 100, 100, 4 ] dimensional. We would input a portion of the multi-dimensional array into the function.

An example is shown below.

----- problematic algorithm --------------

full_x =  rand( 100, 100, 4 );

for ii=1:100000
     myfunc( full_x(:,:,1) );  % copy of full_x(:,:,1) is performed "under the hood"

     myfunc( full_x(:,:,2) );  % the copy of full_x(:,:,1) is not released and being accumulated in memory
     myfunc( full_x(:,:,3) );
     myfunc( full_x(:,:,4) );

end
------------------------------------------

With the above implementation, when passing the input argument into the function Matlab makes a temporary copy of x(:,:,k) and passes the temporary copy into the function (where it is processed). Now the problem is not only the Matlab makes a partial copy of the variable, the garbage collection is not done properly. The temporary copy is still somewhere in Matlab memory taking up space for each call of myfunc(). Because of this Matlab will crash with a "out of memory" error after some time.


A method of coding to go around this issue to pass the ENTIRE variable into the function, along with the index information in which you need to process the variable. An example implementation is shown below.


-----more efficient function-----------------
function [ y ] =  myfunc( x, idx )

% take x and only use the first two dimensions along indice "idx"
% and process something to get results
w = rand( 100, 1 );
y = w'*x(:,:,.idx)*w;

end % end of function
------------------------------------------


----- algorithm fixed ---------------------

full_x = rand( 100, 100, 4 );

for ii=1:100000
     myfunc( full_x, 1 ); % no copy of full_x

     myfunc( full_x, 2 );
     myfunc( full_x, 3 );
     myfunc( full_x, 4 );

end
------------------------------------------


As long as the input variable is not changed in value, Matlab does not copy the entire input variable when calling the function "myfunc()". No copy is performed and thus no memory leakage(?). The only draw-back is that the myfunc() now requires information of the entire variable dimensions, and also require additional information of the index of the variable in which the processing needs to be done.


In many cases this is not going to be an issue, but if handling very large data sets in thousands or millions, this is really an critical issue for stability of the entire code.


No comments:

Post a Comment