From: Ferry on
I have te following problem. In my gui a have a part where I want to show a video stream. I already created a function that returns the images as mxArrays. To set a frame to the gui, I use:
I = GiveFrame(1);
set(imagehandle,'Cdata',I);

Where GiveFrame is my own mexFunction.

So far so good, it works. However, the used memory keeps rising and finally I end up with an out of memory warning.
Is also tried the following:
I = rand(640,480,3);
set(imagehandle,'Cdata',I);

to verify my mexFunction wasn't causing the problem and indeed it results in the same problem. So it seems the set function is the problem. When I remove this line, so I only create random images and not displaying them, everything works fine.
It looks like Matlab uses new memory for every new set statement without cleaning up or something.

I'm using Matlab 7.5.0 R2007b on an Ubuntu 8.10 system.
Anyone knows of this problem? Or is there a way to force Matlab to clean up memory??
From: Steven Lord on

"Ferry " <frietje44(a)hotmail.com> wrote in message
news:i1pd7i$kj6$1(a)fred.mathworks.com...
>I have te following problem. In my gui a have a part where I want to show a
>video stream. I already created a function that returns the images as
>mxArrays. To set a frame to the gui, I use:
> I = GiveFrame(1);
> set(imagehandle,'Cdata',I);
>
> Where GiveFrame is my own mexFunction.
>
> So far so good, it works. However, the used memory keeps rising and
> finally I end up with an out of memory warning.
> Is also tried the following:
> I = rand(640,480,3);
> set(imagehandle,'Cdata',I);
>
> to verify my mexFunction wasn't causing the problem and indeed it results
> in the same problem. So it seems the set function is the problem. When I
> remove this line, so I only create random images and not displaying them,
> everything works fine. It looks like Matlab uses new memory for every new
> set statement without cleaning up or something.
>
> I'm using Matlab 7.5.0 R2007b on an Ubuntu 8.10 system.
> Anyone knows of this problem? Or is there a way to force Matlab to clean
> up memory??

I don't think there's enough information in your posting to be sure that the
SET function is leaking memory. Why don't you show the context in which
you're running this code? For example, if you did something like:


for k = 1:5
hold on
imagehandle = image(NaN);
I = rand(640, 480, 3);
set(imagehandle, 'Cdata', I)
pause(1)
% length(findall(gca, 'Type', 'image'))
end


then MATLAB would consume more and more memory ... perfectly legitimately
(as you're adding a new image to the axes each iteration, as you can see by
uncommenting the last line of the loop.)

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Ferry on
"Steven Lord" <slord(a)mathworks.com> wrote in message <i1pnpr$sge$1(a)fred.mathworks.com>...
>
> "Ferry " <frietje44(a)hotmail.com> wrote in message
> news:i1pd7i$kj6$1(a)fred.mathworks.com...
> >I have te following problem. In my gui a have a part where I want to show a
> >video stream. I already created a function that returns the images as
> >mxArrays. To set a frame to the gui, I use:
> > I = GiveFrame(1);
> > set(imagehandle,'Cdata',I);
> >
> > Where GiveFrame is my own mexFunction.
> >
> > So far so good, it works. However, the used memory keeps rising and
> > finally I end up with an out of memory warning.
> > Is also tried the following:
> > I = rand(640,480,3);
> > set(imagehandle,'Cdata',I);
> >
> > to verify my mexFunction wasn't causing the problem and indeed it results
> > in the same problem. So it seems the set function is the problem. When I
> > remove this line, so I only create random images and not displaying them,
> > everything works fine. It looks like Matlab uses new memory for every new
> > set statement without cleaning up or something.
> >
> > I'm using Matlab 7.5.0 R2007b on an Ubuntu 8.10 system.
> > Anyone knows of this problem? Or is there a way to force Matlab to clean
> > up memory??
>
> I don't think there's enough information in your posting to be sure that the
> SET function is leaking memory. Why don't you show the context in which
> you're running this code? For example, if you did something like:
>
>
> for k = 1:5
> hold on
> imagehandle = image(NaN);
> I = rand(640, 480, 3);
> set(imagehandle, 'Cdata', I)
> pause(1)
> % length(findall(gca, 'Type', 'image'))
> end
>
>
> then MATLAB would consume more and more memory ... perfectly legitimately
> (as you're adding a new image to the axes each iteration, as you can see by
> uncommenting the last line of the loop.)
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>

Hi,
Thx for your answer.
Unfortunately i cannot show you much more of the code.
The whole program is pretty complex and not all the code is my property.

However, i guess the problem could be related to your example. So by setting the colordata of an image to another image, I'm actually adding an image? Than what would be the right way to really 'change' the image?
From: Jan Simon on
Dear Ferry,

> However, i guess the problem could be related to your example. So by setting the colordata of an image to another image, I'm actually adding an image? Than what would be the right way to really 'change' the image?

You can check this by observing the number of children of your figure:
nObject = length(findobj(FigureHandle))
If this is growing while the loop runs, you've found your memory leak.

Good luck, Jan
From: Ferry on
> I don't think there's enough information in your posting to be sure that the
> SET function is leaking memory. Why don't you show the context in which
> you're running this code? For example, if you did something like:
>
>
> for k = 1:5
> hold on
> imagehandle = image(NaN);
> I = rand(640, 480, 3);
> set(imagehandle, 'Cdata', I)
> pause(1)
> % length(findall(gca, 'Type', 'image'))
> end
>
>
> then MATLAB would consume more and more memory ... perfectly legitimately
> (as you're adding a new image to the axes each iteration, as you can see by
> uncommenting the last line of the loop.)
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>

I'm still struggling with this problem. If I add the line:
length(findall(gca, 'Type', 'image')) to my code, the value returned always equals 0.
This is strange as there are more images in my window.
I also tried Jan's suggestion: length(findobj(FigureHandle)), with my own image handle in it, which keeps returning 1.

I even tried length(allchild(gca)), which returns 20 all the time.
So apparently the memory leak doesn't occure because of adding images?

Any suggestions left? Or are there possible tests to see what the memory is filled with?