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 ]
Jesper Holm

Posts: 7
Registered: 10/11/12
Memory leak in Matlab 2012a?
Posted: Oct 11, 2012 3:38 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



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.