MATLAB: Apparent imshow memory leak

imshowmemory leak

Hello,
I have some code that captures and displays images repeatedly for some time, and I noticed that it continually eats up more and more memory (until it crashes).
I did some investigating and provided I don't bother to display the images, there do not appear to be any problems.
I determined that imshow() appears to be the cause of my woes, and I am hoping that there is a technique to resolve the problem.
The following code should demonstrate the issue:
x=rand(500,700,3);
for n=1:10000
imshow(x);
%drawnow;
if mod(n,500) == 0
memory
end
%cla
end
Does anyone know how to stop imshow() from eating up my memory? (or) Does anyone know how to display an image another cleaner way?
Thanks in advance, Sean

Best Answer

You can create the image once and update the CData only afterwards:
x = rand(500,700,3);
imgH = imshow(x);
for n=1:10000
set(imgH, 'CData', x);
drawnow;
if mod(n,500) == 0
memory
end
end
Related Question