Date: Oct 11, 2012 3:38 AM
Author: Jesper Holm
Subject: Memory leak in Matlab 2012a?
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.
t = clock;
Niters = 1000;
memused = zeros(1,Niters);
for iter = 1:Niters
fprintf('Iteration %d of %d\n',iter,Niters);
info = memory;
memused(iter) = info.MemUsedMATLAB;
clearvars -except Niters memused iter t
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);
x = 0:0.01:7;
y = sin(x);
z = cos(x);
figStringA = 'Figure A';
figStringB = 'Figure B';
hFigure2 = figure(200);
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);