From: Mathias Westlund on
Hi,
I am working on a GUI application containing a main while loop controlled by a Start/Stop button. In a certain setting I have found that the memory usage increases for each roundtrip in the program. It is not supposed to and I got suspicious.

Calling the memory function inside my loop indicates that the memory in fact is gradually increasing.
I then suspected that I did something strange while programming so that some parameters was growing for each roundtrip or the number of parameters was growing. However, then I used the following lines of code to sum up all used bytes of all my parameters in the current function:
s=whos();
totalBytes={s.bytes};
sumBytes=0;
for i=1:length(totalBytes)
sumBytes=sumBytes+totalBytes{1,i};
end
sumBytes

sumBytes is constant in the main loop and in all called functions! And the used memory keeps growing.

Even more surprisingly, if I kill the application and call "clear all" the memory is still consumed. I have to kill Matlab to free the memory.

Any tip on how to resolve this or what normally cause these types of memory leakages?
From: Steven Lord on

"Mathias Westlund" <mathias.westlund(a)exfo.com> wrote in message
news:hrc09p$r1t$1(a)fred.mathworks.com...
> Hi,
> I am working on a GUI application containing a main while loop controlled
> by a Start/Stop button. In a certain setting I have found that the memory
> usage increases for each roundtrip in the program. It is not supposed to
> and I got suspicious.

Just because the amount of memory you're using increases doesn't mean
something's leaking memory. Are you perhaps plotting data in your WHILE
loop, using HOLD ON to do so?

x = 0:0.01:2*pi;
axis([0 2*pi -1 1]);
hold on
n = 10;
numberOfChildren = zeros(1, n);
for k = 1:n
line(x, sin(k*x));
numberOfChildren(k) = numel(get(gca, 'Children'));
end
numberOfChildren

At each iteration, you're adding a new line object to the axes, which
requires some memory.

> Calling the memory function inside my loop indicates that the memory in
> fact is gradually increasing.
> I then suspected that I did something strange while programming so that
> some parameters was growing for each roundtrip or the number of parameters
> was growing. However, then I used the following lines of code to sum up
> all used bytes of all my parameters in the current function:
> s=whos();
> totalBytes={s.bytes};
> sumBytes=0;
> for i=1:length(totalBytes)
> sumBytes=sumBytes+totalBytes{1,i};
> end
> sumBytes
>
> sumBytes is constant in the main loop and in all called functions! And the
> used memory keeps growing.
>
> Even more surprisingly, if I kill the application and call "clear all" the
> memory is still consumed. I have to kill Matlab to free the memory.
>
> Any tip on how to resolve this or what normally cause these types of
> memory leakages?

If the above pattern doesn't match what you're doing, then post a small
segment of code that people can run with which you can reproduce this
behavior and/or send that same segment of code to Technical Support along
with the output of the VER function (so they know what version of MATLAB and
other products you're using) for investigation.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ