필터 지우기
필터 지우기

Apparent imshow memory leak?

조회 수: 1 (최근 30일)
Sean
Sean 2011년 11월 17일
댓글: BC 2021년 3월 18일
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
  댓글 수: 1
Jan
Jan 2011년 11월 17일
I've started your example in the command line of Matlab 2009a/64 in Windows 7. Now I cannot stop the program using Ctrl-C. I cannot kill the process due to unsaved changes in some open M-files, but the editor is unresponsive also.
But I do not see an increase in the occupied memory.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2011년 11월 17일
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
  댓글 수: 1
Sean
Sean 2011년 11월 17일
Thanks!
I didn't realize you could do that. It not only eliminated the memory leak, but it updates a whole lot faster too.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2011년 11월 17일
You're basically storing all of those images in the same axes, one on top of another. Put this before imshow and it will be fine:
cla reset;
  댓글 수: 2
Sean
Sean 2011년 11월 17일
I tried what you suggested, and it improved the rate of memory loss from about 10MB per 1000 iterations to about 2MB per 1000 iterations.
Thanks. I'm still going to keep looking for a way to remove the loss altogether though. :)
BC
BC 2021년 3월 18일
Just came here to say I was having issues with a loop giving me an "out of memory" error after a while - including this in my loop seems to have solved this issue for now, thanks!

댓글을 달려면 로그인하십시오.


Yunjin Chen
Yunjin Chen 2017년 6월 21일
from https://stackoverflow.com/questions/36150047/matlab-imshow-how-to-free-memory-but-show-the-image
I bet you are doing something like
hold on
for ii=1:frames
imshow(frame)
drawnow
end
as most memory problems are due to this structure. If you hold on and never clear the figure, you will draw on top of whatever is there, but it will never get deleted. I suggest you remove the hold on if you are just drawing a single thing inside the loop, and if you are drawing more than one thing inside and you need the hold on, then add cla (clear axes) or clf after the drawnow, or in the begging of the loop.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by