Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Memory leak in Matlab 2012a?
Replies: 6   Last Post: Jan 18, 2013 2:44 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Yair Altman

Posts: 1,352
Registered: 3/19/07
Re: Memory leak in Matlab 2012a?
Posted: Oct 11, 2012 6:39 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Jesper Holm" wrote in message <k55t11$qm7$1@newscl01ah.mathworks.com>...
> Hi,
>
> I've been struggling with a MCR executable that runs out of memory during 2-3 weeks of continuous running (we are using MCR for realtime continuous measurements and analysis).
>
> After hours and hours of debugging I was able to get rid of the memory leak by removing annotations from my figures. I've submitted a service request to MathWorks but they are not able to reproduce my problem (the service request is still open so all hope is not lost).
>
> The fact that Mathworks is unable to reproduce my findings troubles me. Are my memory problems related to "external" components (i.e. java installation etc)?
>
> I'm therefore hoping that some of you would try to run the script below and let me know what you see. The Niters variable determines the number of loops. It's currently set to 1000 but feel free to modify it. The example is based on suggestions from MathWorks and Niters have to be large enough to see a clear trend.
>
> The script uses the memory() function which is only available in later Matlab versions.
> On Matlab 2012a (which is what we use currently) I see a 20Mb increase in used matlab memory after the test run. This memory is never released - you have to quit Matlab before getting it back. I've also run a few tests on 2012b. Here, the problem is not so large but I still see an increase in used memory (3-5Mb).
>
> Anyway.. The script is presented below. Sorry if the formatting gets screwed up. If I comment the two annotation lines the problem goes away on my setup.
>
> ---------------------
>
> function memleak3()
>
> clear all;
> close all;
> clc;
>
> t = clock;
> Niters = 1000;
> memused = zeros(1,Niters);
> for iter = 1:Niters
> fprintf('Iteration %d of %d\n',iter,Niters);
>
> runLeakingCode();
>
> info = memory;
> memused(iter) = info.MemUsedMATLAB;
> close all;
> clc;
> clearvars -except Niters memused iter t
> end
>
> plot(1:Niters,memused,'r-o');
> grid on;
> fprintf('Max = %d, Min = %d, Diff = %d\n',max(memused),min(memused),max(memused)-min(memused));
>
> t = fix(etime(clock,t));
> fprintf('Elapsed time = %d seconds..\n',t);
> end
>
> function runLeakingCode()
> x = 0:0.01:7;
> y = sin(x);
> z = cos(x);
>
> figStringA = 'Figure A';
> figStringB = 'Figure B';
>
> hFigure2 = figure(200);
> clf;
> set(hFigure2,'name',figStringA);
> annotation('textbox',[0 0.00 1 0.05],'VerticalAlignment','bottom','FitHeightToText','off','LineStyle','none','Margin',2,'String',figStringA,'FontSize',7);
> annotation('textbox',[0 0.95 1 0.05],'VerticalAlignment','top','FitHeightToText','off','LineStyle','none','Margin',2,'String',figStringB,'FontSize',7);
> subplot(2,1,1);
> plot(x,y);
> grid on;
> subplot(2,1,2);
> plot(x,z);
> grid on;
> end



Instead of closing your figures within the loop, try to clf the current figure and reuse the same figure window. This would also be better for performance. Even better, reuse all the internal handles as well (annotations, axes) such that instead of clf you would simply modify their internal properties - this would run much faster and would also remove the leak.

I suspect that the current memory leak that you see may be due to the fact that Matlab does not properly destroy the figures' resources upon closing, and 20KB per iteration (=per figure) seems about right.

Yair Altman
http://UndocumentedMatlab.com




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.