Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Cell Array of Function Handles Leaks Memory
Date: Sun, 1 Jun 2008 04:40:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 36
Message-ID: <g1t973$aeg$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1212295204 10704 172.30.248.37 (1 Jun 2008 04:40:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 1 Jun 2008 04:40:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1327593
Xref: news.mathworks.com comp.soft-sys.matlab:471598



Hi,

I am running Matlab version 7.0.0.19920 (R14) under Windows
XP and it seems to be leaking memory when I create a cell
array of function handles within an m-file.

In particular, writing the function:

function A = cellArrayFunHandleLeaks
A = cell(1,1);
B = ones(1000,1000);
A{1} = @(x) x+B;

and calling from the command line:
A = cellArrayFunHandleLeaks; clear A;

leaks about 7.5MB of memory (I am using the MATLAB
monitoring tool available at
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7127&objectType=file
to measure memory usage, but an increase in memory allocated
to MATLAB is clearly visible from Windows Task Manager).

However, if I run the commands in the function from the
command prompt and then do clear A B; I am able to recover
all of the memory.  Similarly, if I do A = []; at the end of
the above function, there is no memory leak.

I observe no memory leak with a cell array of matrices or
just a single function handle not in a cell array, when used
as above.

Has anybody else had this problem or know of a solution or
of a better way to store a set of function handles?

Thanks in advance,
Nick