Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Memory usage issues when printing figures in a loop.

Subject: Memory usage issues when printing figures in a loop.

From: Juan

Date: 25 Feb, 2010 21:19:04

Message: 1 of 5

I have the following problem. I need to make movies that consist of trajectories on a plane. I have tried to use avifile and getframe with unsatisfactory results. The files are too big and because I am running Matlab R2009a on Mac OS X 10.6.2 I do not have any compression codecs available to me in Matlab. Furthermore, I run into memory problems. I decided to generate encapsulated post-script files and then use tools such as convert and mencoder to generate high-quality movies. However, in order to produce these movies I need to generate 3251 frames, or 3251 figures. In each frame the particle position changes. I plot the particle position in each frame using the function scatter because I want to color code the marker on the figure according to the particle acceleration. The problem I am having is that the memory usage seems to increase (although I am not certain of this because the
function memory is not available on my platform) and Matlab freezes after a while. I am able to generate all the files for one particle, but then even if issue "clear all" and run the script again, eventually it freezes. Below is the relevant code. Notice that I have a background that does not change. This plot is made just once and then only the particle position is updated. At the end of every iteration delete the current particle position and some text that indicates time elapsed. Any help is greatly appreciated. I would like to be able to generate several trajectories without having to shutdown matlab and restart the computer.

--------
visible='off';
font='Palatino';

fig=figure('Visible',visible);
h=streamslice(X,Y,R,Q,10,'method','cubic');
set(h,'Color','k');
hold on;

for i=istart:istop
    if i==istart
        colormap(jet);
    end
    s=scatter(QW_agg(i),-QS_agg(i),30,c(i),'filled');
    if i==istart
        axis([0 20 0 20])
        cbar=colorbar('peer',gca,'FontName',font,'FontSize',12,'FontWeight','bold');
        caxis([0 5]);
        ylabel(cbar,'$|a|/a_{\mathrm{rms}}(St)$','Interpreter','latex','FontName',font,...
                'FontSize',14,'Rotation',90);
        t=title(strcat('$St=$',num2str(St_agg(i),'%4.3f')));
        set(t,'Interpreter','latex','FontName',font,'FontSize',14);
     end
     txt=text(13,19,strcat('$t/\tau_{\mathrm{eddy}}=...
         $',num2str(i*tracktime*dt/tau_eddy,'%1.2f')));
    set(txt,'Interpreter','latex','FontName',font,'FontSize',14);
    print(fig,strcat(dirname,'/',num2str(i,'%05.0f')),'-depsc');
    delete(txt);
    delete(s);
end

Subject: Memory usage issues when printing figures in a loop.

From: Ravi ch

Date: 25 Feb, 2010 21:48:05

Message: 2 of 5

juan

did u try closing the figure after generating the .eps file?..shud look something like this...each open figure takes up a lot of memory..let me know

..
..
print(fig,strcat(dirname,'/',num2str(i,'%05.0f')),'-depsc');
close(fig)
delete(txt);
..
..


"Juan " <js522@cornell.edu.butnothispart> wrote in message <hm6pg8$4nf$1@fred.mathworks.com>...
> I have the following problem. I need to make movies that consist of trajectories on a plane. I have tried to use avifile and getframe with unsatisfactory results. The files are too big and because I am running Matlab R2009a on Mac OS X 10.6.2 I do not have any compression codecs available to me in Matlab. Furthermore, I run into memory problems. I decided to generate encapsulated post-script files and then use tools such as convert and mencoder to generate high-quality movies. However, in order to produce these movies I need to generate 3251 frames, or 3251 figures. In each frame the particle position changes. I plot the particle position in each frame using the function scatter because I want to color code the marker on the figure according to the particle acceleration. The problem I am having is that the memory usage seems to increase (although I am not certain of this because the
> function memory is not available on my platform) and Matlab freezes after a while. I am able to generate all the files for one particle, but then even if issue "clear all" and run the script again, eventually it freezes. Below is the relevant code. Notice that I have a background that does not change. This plot is made just once and then only the particle position is updated. At the end of every iteration delete the current particle position and some text that indicates time elapsed. Any help is greatly appreciated. I would like to be able to generate several trajectories without having to shutdown matlab and restart the computer.
>
> --------
> visible='off';
> font='Palatino';
>
> fig=figure('Visible',visible);
> h=streamslice(X,Y,R,Q,10,'method','cubic');
> set(h,'Color','k');
> hold on;
>
> for i=istart:istop
> if i==istart
> colormap(jet);
> end
> s=scatter(QW_agg(i),-QS_agg(i),30,c(i),'filled');
> if i==istart
> axis([0 20 0 20])
> cbar=colorbar('peer',gca,'FontName',font,'FontSize',12,'FontWeight','bold');
> caxis([0 5]);
> ylabel(cbar,'$|a|/a_{\mathrm{rms}}(St)$','Interpreter','latex','FontName',font,...
> 'FontSize',14,'Rotation',90);
> t=title(strcat('$St=$',num2str(St_agg(i),'%4.3f')));
> set(t,'Interpreter','latex','FontName',font,'FontSize',14);
> end
> txt=text(13,19,strcat('$t/\tau_{\mathrm{eddy}}=...
> $',num2str(i*tracktime*dt/tau_eddy,'%1.2f')));
> set(txt,'Interpreter','latex','FontName',font,'FontSize',14);
> print(fig,strcat(dirname,'/',num2str(i,'%05.0f')),'-depsc');
> delete(txt);
> delete(s);
> end

Subject: Memory usage issues when printing figures in a loop.

From: Juan

Date: 25 Feb, 2010 22:48:04

Message: 3 of 5

"Ravi ch" <ravi.chilumula@gmail.com> wrote in message <hm6r6l$o09$1@fred.mathworks.com>...
> juan
>
> did u try closing the figure after generating the .eps file?..shud look something like this...each open figure takes up a lot of memory..let me know
>
> ..
> ..
> print(fig,strcat(dirname,'/',num2str(i,'%05.0f')),'-depsc');
> close(fig)
> delete(txt);
> ..

The reason I didn't do that is that if I close the figure I will loose the background and hence will need to plot the background again for the next frame (which would make things slow down considerably). Since I am always plotting to the same figure (I used "hold on"), I am not creating new ones. I am not sure how the memory allocation works, but it seems that when I plot a new point on the figure after having deleted the previous point, even if it is in the same figure, additional memory is gobbled up. Why does this happen?

Subject: Memory usage issues when printing figures in a loop.

From: Liz

Date: 14 Nov, 2011 02:59:11

Message: 4 of 5

Hi Juan,

did you ever figure out what the problem was? I have a similar issue. Thanks

Liz

Subject: Memory usage issues when printing figures in a loop.

From: Camille Couzi

Date: 13 Mar, 2012 02:07:12

Message: 5 of 5

Hi Liz and Juan,
Did you find a solution to this problem? I am having it now too :-)
Thanks a lot for your help!
Camille.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us