Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Cell Array of Function Handles Leaks Memory

Subject: Cell Array of Function Handles Leaks Memory

From: NIcholas

Date: 1 Jun, 2008 04:40:04

Message: 1 of 11

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

Subject: Cell Array of Function Handles Leaks Memory

From: Bruno Luong

Date: 1 Jun, 2008 07:51:02

Message: 2 of 11

"NIcholas " <remove.this_nmg33@cornell.edu> wrote in message
<g1t973$aeg$1@fred.mathworks.com>...

> A = cellArrayFunHandleLeaks; clear A;
>

Can you use "clear all"? I'm not sure what Matlab doing for
keeping last current commant prompt, debugging information,
"precompiled" code, path, etc ... Beware about interpreting
"MATLAB memory".

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

Alternatively, you can put it in a structure array.

Bruno

Subject: Cell Array of Function Handles Leaks Memory

From: NIcholas

Date: 1 Jun, 2008 15:03:06

Message: 3 of 11

"Bruno Luong" <b.luong@fogale.fr> wrote in message
<g1tkd6$qi9$1@fred.mathworks.com>...
> "NIcholas " <remove.this_nmg33@cornell.edu> wrote in message
> <g1t973$aeg$1@fred.mathworks.com>...
>
> > A = cellArrayFunHandleLeaks; clear A;
> >
>
> Can you use "clear all"? I'm not sure what Matlab doing for
> keeping last current commant prompt, debugging information,
> "precompiled" code, path, etc ... Beware about interpreting
> "MATLAB memory".
>
> >
> > Has anybody else had this problem or know of a solution or
> > of a better way to store a set of function handles?
> >
>
> Alternatively, you can put it in a structure array.
>
> Bruno


Hi Bruno thanks for your suggestions.

I run into similar problems using a structure array to store
variables:

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

A = cellArrayFunHandleLeaks; clear all;
gives a similar problem, and so does running the first
commented line instead. The last commented line runs fine
though.

Also, I am not even using B in the anonymous function. Does
MATLAB store copies of all variables in the local scope when
creating an anonymous function?

The following code without the clear B line has a similar
problem, but with clear B (even after anonymous function
declaration) it works fine.

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

Subject: Cell Array of Function Handles Leaks Memory

From: us

Date: 1 Jun, 2008 15:21:01

Message: 4 of 11

"NIcholas ":
<SNIP down to partial answer...

> Does MATLAB store copies of all variables in the local
scope when creating an anonymous function...

yes, as can be seen here

% create a dummy function
function r=foo
     b=ones(100);
     r.f=@(x) x=1;
end

% at the command prompt
     f=foo;
     whos f
% f 1x1 140 struct <- apparently small
     ff=functions(f.f);
     whos ff
% ff 1x1 81090 struct <- reveal the truth
     ff
%{
     function: '@(x)x+1'
     type: 'anonymous'
     file: 'YOURPATH\foo.m'
     workspace: {2x1 cell}
%}
     ff.workspace{2}
%{
     r: [1x1 struct]
     b: [100x100 double] % in foo's ws at time of creation
%}

us

Subject: Cell Array of Function Handles Leaks Memory

From: NIcholas

Date: 1 Jun, 2008 15:56:01

Message: 5 of 11

"us " <us@neurol.unizh.ch> wrote in message
<g1ueot$67i$1@fred.mathworks.com>...
> % create a dummy function
> function r=foo
> b=ones(100);
> r.f=@(x) x=1;
> end

I assumed this should be x+1, since x=1 gives illegal
assignment error.

> % at the command prompt
> f=foo;
> whos f
> % f 1x1 140 struct <- apparently small

> ff=functions(f.f);
> whos ff
> % ff 1x1 81090 struct <- reveal the truth
> ff
> %{
> function: '@(x)x+1'
> type: 'anonymous'
> file: 'YOURPATH\foo.m'
> workspace: {2x1 cell}
> %}

I get the same result for whos f, but I get different
results for ff:
whos ff
% ff 1x1 572 struct array

ff
%{
function: '@(x) x+1'
         type: 'anonymous'
         file: 'C:\MATLAB7\work\foo.m'
         workspace: [1x1 struct]

%}

ff.workspace
%{
1x1 struct array with no fields.
%}

However, changing the function to: r.f=@(x) x+b;
I get something similar:
whos ff
% ff 1x1 80696 struct array
ff.workspace
% b: [100x100 double]

Subject: Cell Array of Function Handles Leaks Memory

From: us

Date: 1 Jun, 2008 16:14:02

Message: 6 of 11

"NIcholas ":
<SNIP gets different results...

> > r.f=@(x) x=1;
> I assumed this should be x+1, since x=1 gives illegal
> assignment error...

CORRECT! sorry for the typo...

> I get the same result for whos f, but I get different
> results for ff
> ff
> function: '@(x) x+1'
> type: 'anonymous'
> file: 'C:\MATLAB7\work\foo.m'
> workspace: [1x1 struct]
>

what ML version do you have?
we run 2007b...

us

Subject: Cell Array of Function Handles Leaks Memory

From: NIcholas

Date: 1 Jun, 2008 16:20:02

Message: 7 of 11

"us " <us@neurol.unizh.ch> wrote in message
<g1uhsa$pi4$1@fred.mathworks.com>...
> "NIcholas ":
> <SNIP gets different results...

> what ML version do you have?
> we run 2007b...
>
> us

I am running Matlab version 7.0.0.19920 (R14) under Windows
XP. Do you get a memory leak using the code I posted above
under 2007b?

Subject: Cell Array of Function Handles Leaks Memory

From: us

Date: 1 Jun, 2008 16:38:01

Message: 8 of 11

"NIcholas ":
<SNIP mem leak...

> I am running Matlab version 7.0.0.19920 (R14) under
Windows XP.
> Do you get a memory leak using the code I posted above
> under 2007b...

no, not here; system:
ic2.2*2.3mhz/2g/winxp.sp2/2007b(7.5.0.342)

this might(!) indicate that the memory, which is visibly
attached to the function handle in 2007b is hidden in the
older version...
therefore, this might(!) help

     clear cellArrayFunHandleLeaks;

just a thought
us

Subject: Cell Array of Function Handles Leaks Memory

From: NIcholas

Date: 1 Jun, 2008 17:42:01

Message: 9 of 11

"us " <us@neurol.unizh.ch> wrote in message
<g1uj99$4tr$1@fred.mathworks.com>...
> "NIcholas ":
> <SNIP mem leak...
>
> > I am running Matlab version 7.0.0.19920 (R14) under
> Windows XP.
> > Do you get a memory leak using the code I posted above
> > under 2007b...
>
> no, not here; system:
> ic2.2*2.3mhz/2g/winxp.sp2/2007b(7.5.0.342)
>
> this might(!) indicate that the memory, which is visibly
> attached to the function handle in 2007b is hidden in the
> older version...
> therefore, this might(!) help
>
> clear cellArrayFunHandleLeaks;
>
> just a thought
> us

I still have problems after using the clear
cellArrayFunHandleLeaks;

I have come up with a solution that would be acceptable if
there is no way to resolve this issue in older version of
MATLAB.

I am storing a structure array with a field for data
required by the function, and a field for the function,
written as a simple function in an m-file rather than an
anonymous function. The function would be called by passing
in the desired argument followed by the data stored in data
field.

For the above example the fix would be:

function z=add(x,y)
    z=x+y;
end

function A = cellArrayFunHandleLeaks(method)
    B=ones(1000,1000);
    if method==1
        A.f = @(x) x+B;
    else
        A.f = @add;
        A.data = {B};
    end
end

% leaks memory
A = cellArrayFunHandleLeaks(1);
argIn = 1;
result = A.f(1);
clear all;

% does not leak memory
A = cellArrayFunHandleLeaks(0);
argIn = 1;
result = A.f(1, A.data{:});
clear all;

The downside of this solution is that another m-file is
needed for each unique function and code for calling the
function is slightly longer.

Subject: Cell Array of Function Handles Leaks Memory

From: Bruno Luong

Date: 1 Jun, 2008 20:28:02

Message: 10 of 11


> The downside of this solution is that another m-file is
> needed for each unique function ...

You might nest "add" into "cellArrayFunHandleLeaks". Though
it's not very nice because of the leakage.

No leakage problem for my 2008a.

Good luck,

Bruno

Subject: Cell Array of Function Handles Leaks Memory

From: Mike Karr

Date: 13 Jun, 2008 16:49:24

Message: 11 of 11

Bruno Luong wrote:

 > (snip)
> No leakage problem for my 2008a.
 > (snip)

If I follow this thread topic correctly, there are no known leakage
problems in 2008a. There were indeed leakage problems in some earlier
versions that we fixed. As you might imagine, there is really no way
that we can go back and fix those earlier problems.

If you do find leaks, of any kind, in a current version of MATLAB,
either now or in the future, please report them to the MathWorks.

thanks,
mike

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us