Why such Increase in used memory when using figures within a loop

7 views (last 30 days)
Hello,
I've a problem with increasing memory when using open figures within a loop. My application continously reads data from disk within a loop, processes the data and plots the results as stairs, plot and image within 5 different figures. The figures are opened when initializing the processing. The figure handles are saved and used with the command figure(handle1), .. figure(handle5) to plot the data after each processing step within the loop. Since a large amount of data will be processed and although I control the memory of the application (clear var1, var2, ...), the memory (seen in the Windows task manager) increases and increases and ... . After a while MATLAB crashes with Java errors after the application gets slower and slower and ... . If the amount of data is smaller, no crash occurs, but memory has been increased e.g. from 160MB to 400MB. After "close all", this goes down to less than 160 MB. After "clear all" memory increases to about 200 MB. It's a mystery to me. Is there anyone with similar problems? How to explain these effects and how to treat these? Any solution for this?
  1 Comment
Bernhard
Bernhard on 13 Sep 2011
@Jan: thanks for your answer and hint how to answer. I'll try to do so.
I will attach a strongly reduced code only to show the flowgraph of the program. This reduced code is not running! The function "processing" is called with a dedicated set of data.
function [out1, out2] = processing(in1, in2, in3)
After some lines of code filenames and the figures are initialized by:
if(Plot)
.
.
filename_fig2 = [directory '\References\Adata'];
filename_fig3 = [directory '\References\Bdata'];
fh2 = figure(2);
clf(fh2,'reset');
set(fh2,'Position',[22 80 393 420]);
set(fh2,'Name','Average A per proc. step');
set(fh2,'Visible','off')
fh3 = figure(3);
clf(fh3,'reset');
set(fh3,'Position',[428 80 393 420]);
set(fh3,'Name','Average A per proc. step');
set(fh3,'Visible','off')
.
.
.
.
.
end
Next comes the loop with the proceesing of data, which a read from file.
k = 0;
for current_data = start_data:last_data
.
.
.
k = k + 1;
--- compute current time ---
t = (k-1) * dt;
--- call processing ---
[datalist,varlist] = processing_control(var1, varn);
end
After the complete set of data has been processed, some results
are plot, using:
if(Plot)
--- prepare title ---
sa = sprintf('bla, bla, bla');
set(fh2,'Visible','on')
--- plot information ---
figure(fh2);
subplot(211)
plot(datalist.A);grid on
ylabel('Avg A]')
title(sa)
subplot(212)
plot(datalist.B);grid on
xlabel('proc step')
ylabel('Avg B]')
.
.
.
.
.
saveas(fh2,filename_fig2,'jpg');
fprintf(1,' --> Saved %s.jpg\n',filename_fig2);
saveas(fh3,filename_fig3,'jpg');
fprintf(1,' --> Saved %s.jpg\n',filename_fig3);
delete(fh2);
delete(fh3);
.
.
.
.
end
Finally in the current version all remaining figures are deleted by:
h = findobj(findobj,'-depth',0,'Type','Figure');
if(~isempty(h))
for lh=1:length(h)
delete(h(lh));
end
end
Last the end of the function by:
end
The function processing_control calls another function, which process a sub-set of data and plots some results within 3 figures. This function is called about 15 times per complete dataset.
function [somevalues] = anotherfunction(somevalues)
.
.
.
After some processing the figures are initialized:
if(Plot)
if(FirstCall)
fh67 = figure(67);
clf(fh67,'reset');
set(fh67,'Position',[579 580 338 370]);
set(fh67,'Name','Filtered ...');
fh68 = figure(68);
clf(fh68,'reset');
set(fh68,'Position',[929 580 338 370]);
set(fh68,'Name','Correlation ...');
fh69 = figure(69);
clf(fh69,'reset');
set(fh69,'Position',[831 130 436 370]);
set(fh69,'Name','Average ...');
The figure handles are saved in somevalues.
end
end
.
.
.
.
After some processing the first figure of three is called:
if(Plot)
%
figure(somevalues.figurehandles.fh69);
stairs(mean_....);grid on
hold on
stairs(sum_...,'b');grid on
xlabel('aaaa')
ylabel('bbb')
title('average ...')
legend('aaa','bbb','Location','SouthEast','Location','NorthEast')
drawnow
hold off
end
.
.
.
.
end

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 8 Sep 2011
You do not mention in the above description that you are delete()'ing the handle graphics objects. Clearing a variable that a handle is stored in does not delete the object.
But beyond that: there have been reports that would appear to be consistent with the possibility that there is a memory leak in the handle graphics system involving some interaction with Java. I do not recall reading that it has been officially proven yet.

Bernhard
Bernhard on 9 Sep 2011
Thanks Walter. I have used "close(figurehandle)" to delete the figure. As to your answer I have changed this to "delete(figurehandle)", because "close" does not a deletion. The second point, the increase in memory. I use 5 figure per processing step. Used memory increases by 1MB per processing step.
  1 Comment
Jan
Jan on 9 Sep 2011
Can you post a relevant part of your code, which reproduces the problem? Without seeing the code, we can only guess, what the problem might be.

Sign in to comment.


Bernhard
Bernhard on 12 Sep 2011
Hello Jan,
sorry, because it's my first time, how can I attach a piece of code to an answer? I have tried this, but have got a lot of problems with formatting.
  1 Comment
Jan
Jan on 12 Sep 2011
Please use comments to comment comments or answers, and not a new answer.
Insert the code in the original question by editing it, because it is not an "answer" to your problem.
See: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!